美文网首页
Recyclerview简单效果(水平方向第一个和最后一个加间距

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

作者: GiottoYLY | 来源:发表于2020-11-27 17:34 被阅读0次

原文: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);

        //写这边的时候我遇到了一个小问题,就是 state.getItemCount()和parent.getChildCount()的值
        //我的list给的值是8条数据,运行起来,屏幕上一开始展示出的是4个item
        //在这边判断是不是最后一个item的时候,这里应该取state.getItemCount()的值
        int listSize = state.getItemCount();//listSize 这里打印出的值是8
//        CMULog.d("listSize = " + listSize);
        int childCount = parent.getChildCount();//这里打印出的值是4
//        CMULog.d("childCount = " + childCount);

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

            if (orientation == LinearLayoutManager.VERTICAL) {
                //垂直
                float top = view.getPaddingTop();
                float bottom = view.getBottom() + drawPadding;
                float left = view.getPaddingLeft();
                float right = view.getWidth() - view.getPaddingRight();
                c.drawRect(left, top, right, bottom, paint);

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

调用

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

相关文章

网友评论

      本文标题:Recyclerview简单效果(水平方向第一个和最后一个加间距

      本文链接:https://www.haomeiwen.com/subject/cuoxwktx.html