Recyclerview的ItemDecoration简单使用

原文:Android——RecyclerView入门学习之ItemDecoration

image.png

简单效果:水平方向第一个和最后一个加间距

项目里有一个水平滑动的RecyclerView,第一个和最后一个距离手机两边间距大一点,其他的间距都靠加的背景图带了点距离撑开
以前的我都是在item里左右都放一个View,判断第0个的时候显示左边的View,最后一个显示右边的View,其他的两个View都隐藏,方法很笨,但是也实现了
但是强大的RecyclerView会教你优雅的实现间距,就是 ItemDecoration

看了文章写了一个可以实现上述效果的一个简单ItemDecoration,这里记录一下

/**
 *水平滑动的RecyclerView:第0个和第size-1个加间距,其他的不加
 *垂直滑动的RecyclerView:每个底部加36的间距
 */
public class RecyclerViewLeftRightAddPadding extends RecyclerView.ItemDecoration {

    private int drawPadding = 36;
    private int orientation;
    private Paint paint;

    public RecyclerViewLeftRightAddPadding(Context context, int orientation) {

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(context.getResources().getColor(R.color.white));

        this.orientation = orientation;
    }

     @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        //加一个偏移量

        RecyclerView.LayoutManager manager = parent.getLayoutManager();
        int index = parent.getChildAdapterPosition(view);
        int listSize = state.getItemCount();

        if (manager instanceof LinearLayoutManager) {
            if (orientation == LinearLayoutManager.VERTICAL) {
                //垂直
                outRect.set(0, 0, 0, drawPadding);
            } else {
                //水平
                if (index == 0) {
                    outRect.set(drawPadding, 0, 0, 0);
                } else if (index == listSize - 1) {
                    outRect.set(0, 0, drawPadding, 0);
                } else {
                    outRect.set(0, 0, 0, 0);
                }
            }
        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);

        int listSize = state.getItemCount();
//        LogUtil.d("listSize = " + listSize);
        int childCount = parent.getChildCount();
//        LogUtil.d("childCount = " + childCount);

        for (int i = 0; i < childCount; i++) {
            View view = parent.getChildAt(i);
            int index = parent.getChildAdapterPosition(view);
//            LogUtil.d("index = " + index);

            if (orientation == LinearLayoutManager.VERTICAL) {
                //垂直
                float top = view.getBottom();
                float bottom = view.getBottom() + drawPadding;
                float left = view.getLeft();
                float right = view.getRight();
                c.drawRect(left, top, right, bottom, paint);

            } else {
                //水平
                if (index == 0) {
                    c.drawRect(view.getLeft() - drawPadding, view.getTop(), view.getLeft(), view.getBottom(), paint);
//                    LogUtil.d("left = " + (view.getLeft() - drawPadding) + ",top = " + top + ",right = " + view.getRight() + ",bottom = " + bottom);
                } else if (index == listSize - 1) {
                    c.drawRect(view.getRight(), view.getTop(), view.getRight() + drawPadding, view.getBottom(), paint);
//                    LogUtil.d("left = " + view.getLeft() + ",top = " + top + ",right = " + (view.getRight() + drawPadding) + ",bottom = " + bottom);
                } else {
                    c.drawRect(view.getLeft(), view.getTop(), view.getLeft(), view.getBottom(), paint);
//                    LogUtil.d("left = " + view.getLeft() + ",top = " + top + ",right = " + view.getRight() + ",bottom = " + bottom);
                }
            }
        }
 
    }

调用

        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.addItemDecoration(new RecyclerViewLeftRightAddPadding(this, LinearLayoutManager.HORIZONTAL));
        horizontalAdapter = new RecyclerViewHorizontalAdapter(this, list);
        recyclerView.setAdapter(horizontalAdapter);

推荐阅读更多精彩内容