自定义 RecyclerView.ItemDecoration

public class MyItemDecoration extends RecyclerView.ItemDecoration{

    public MyItemDecoration() {
        super();
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        KJLoger.debug("==================onDraw==================");
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        KJLoger.debug("==================onDrawOver==================");
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        KJLoger.debug("==================getItemOffsets==================");
    }
}


执行,可看见执行顺序,如图:


所以,首先看:

1. getItemOffsets该方法,该方法中,我们主要修改参数outRect,该参数可控制I每个tem项的上、下、左、右间隔(相当于控制Item显示位置)。在之前先给每个Item设置背景颜色,例:

修改前:

public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    Log.i("debug", "==================getItemOffsets==================");
    
}



修改之后:

public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);
    Log.i("debug", "==================getItemOffsets==================");
    outRect.set(10,10,10,10);
}




2. onDraw:负责Item的绘制,可以在原始Item的基础上进行添加绘制。(此方法中的绘制图案并不会覆盖元有图案,只是相当于透明蒙版(带色玻璃)一样覆盖在上层),该方法可用于:绘制分割线,附带Item蒙版等。

如下代码是添加绘制蓝色蒙版:

@Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        Log.i("debug", "==================onDraw==================");
        //计算子元素实际左右坐标
        int left = parent.getPaddingLeft()+itemSpace;
        int right = parent.getWidth() - parent.getPaddingRight()-itemSpace;
        int top = 0;
        int bootom = 0;

        int count = parent.getChildCount();

        for (int i = 0; i < count; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
//            top = child.getBottom();
            top = child.getTop();
            bootom = child.getBottom();
            c.drawRect(left,top,right,bootom,paint);

        }
    }

效果如图:(其他效果可自行尝试)



3. onDrawOver:该方法同样是用于Item的绘制,与onDraw不同的是该方法所绘制的东东会覆盖onDraw以及之前的基础图案之上,与上面同样的代码:

@Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        Log.i("debug", "==================onDrawOver==================");
        //计算子元素实际左右坐标
        int left = parent.getPaddingLeft()+itemSpace;
        int right = parent.getWidth() - parent.getPaddingRight()-itemSpace;
        int top = 0;
        int bootom = 0;

        int count = parent.getChildCount();

        for (int i = 0; i < count; i++) {
            View child = parent.getChildAt(i);
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
//            top = child.getBottom();
            top = child.getTop();
            bootom = child.getBottom();
            c.drawRect(left,top,right,bootom,paint);
        }
    }

此时我们再来看看效果:



此时,我们只能看见一片蓝,因为之前的东东被onDrawOver里面的东东给覆盖了。


这就差不多是这三个方法的大致用法了,当然各种用途就要靠大家自我开发了。


源码下载








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值