Android-自定义瀑布流的itemDecoration

point1

继承RecyclerView.ItemDecoration主要有以下三个方法:

  1. getItemOffsets(rect ,view ,recyclerView,state);

​ 设置绘制区域rect.set(x,x,x,x)。

  1. ondraw(cavas,recyclerview,state)

​ 找好可以绘制的区域,用cavas绘制。先于itemView的ondraw方法。

  1. ondrawover(cavas,recyclerview,state);

​ 这个跟ondraw差不多,后于itemView的ondraw方法。

point2 瀑布流的decoration

1
2
3
4
5
6
7
8
9
StaggeredGridLayoutManager.LayoutParams lp = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();
int spanIndex = lp.getSpanIndex();
Logger.d("test>>>",position+"======"+spanIndex);

if(spanIndex%2==0){//left
outRect.set(0,0,mVerticalSpan,mVerticalSpan);
}else{//right
outRect.set(0,0,0,mVerticalSpan);
}

两列的时候,spanIndex要么为0,要么为1,0表示在左边,1表示在右边。