public class TestItemDecorationextends RecyclerView.ItemDecoration {
DrawablemDrawable;
public TestItemDecoration(Context context, int resourceId) {
super();
mDrawable = context.getDrawable(resourceId);
}
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
int count = parent.getChildCount();
Rect rect =new Rect();
rect.left = parent.getPaddingLeft(); //设置左边坐标
rect.right = parent.getWidth() - parent.getPaddingRight();//设置右边坐标
for(int i=1;i
rect.bottom = parent.getChildAt(i).getTop(); //设置底部 底部= 第1 个 item的顶部
rect.top = rect.bottom -mDrawable.getIntrinsicHeight(); //设置顶部
mDrawable.setBounds(rect);
mDrawable.draw(canvas); //绘画
}
}
/**
* 留出分割线位置
* @param outRect
* @param view
* @param parent
* @param state
*/
@Override
public void getItemOffsets( Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if(parent.getChildAdapterPosition(view)!=0){
outRect.top =mDrawable.getIntrinsicHeight();
}
}
}