美文网首页
RecyclerView的ItemDecoration

RecyclerView的ItemDecoration

作者: 竹叶儿青 | 来源:发表于2020-07-01 17:23 被阅读0次

    根据设置的不同LayoutManager,设置不同的距离

    public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
    
        public static final int ORIENTATION_HORIZONTAL = 0;
        public static final int ORIENTATION_VERTICAL = 1;
        public static final int ORIENTATION_GRID = 2;
    
        private int orientation;
        private int leftSpace;
        private int topSpace;
        private int column;
    
        public static SpaceItemDecoration newHorizontalDecoration(int space) {
            return new SpaceItemDecoration(ORIENTATION_HORIZONTAL, space, 0, 0);
        }
    
        public static SpaceItemDecoration newVerticalDecoration(int space) {
            return new SpaceItemDecoration(ORIENTATION_VERTICAL, 0, space, 0);
        }
    
        public static SpaceItemDecoration newGridDecoration(int column, int leftSpace, int topSpace) {
            return new SpaceItemDecoration(ORIENTATION_GRID, leftSpace, topSpace, column);
        }
    
        public SpaceItemDecoration(int orientation, int leftSpace, int topSpace, int column) {
            this.orientation = orientation;
            this.leftSpace = leftSpace;
            this.topSpace = topSpace;
            this.column = column;
        }
    
        @Override
        public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
            outRect.left = leftSpace;
            outRect.top = topSpace;
            switch (orientation) {
                case ORIENTATION_HORIZONTAL:
                // 水平的LayoutManager,设置第一个item与左边界紧挨
                    if (parent.getChildAdapterPosition(view) == 0) {
                        outRect.left = 0;
                    }
                    break;
                case ORIENTATION_VERTICAL:
                // 垂直的LayoutManager,设置第一个item与上边界紧挨
                    if (parent.getChildAdapterPosition(view) == 0) {
                        outRect.top = 0;
                    }
                    break;
                case ORIENTATION_GRID:
                // 网格布局的LayoutManager,设置每一行的第一个item与左边界        
                //紧挨,每一行最后一个item与右边界紧挨,其余自动向右侧偏移
                    int mod = parent.getChildAdapterPosition(view) % column;
                    outRect.left = leftSpace * mod;
                    break;
            }
        }
    
    }
    

    调用:

    // 水平/垂直布局
    LinearLayoutManager manager = new LinearLayoutManager(mBaseActivity);
            manager.setOrientation(RecyclerView.HORIZONTAL);
    
    recyclerView.addItemDecoration(SpaceItemDecoration.newHorizontalDecoration(
                    DimensionUtil.dp2px(this, DimensionUtil.DEFAULT_RECYCLER_VIEW_ITEM_SPACE)));
    
    // 网格布局
    // 获取屏幕宽度,减去padding/margin等等,得到recyclerView的实际宽度
    // 这里如果直接使用recyclerView.getWidth()可能为0,需要注意
    // 然后均分成 column * (column - 1) 份,用于偏移
    GridLayoutManager manager = new GridLayoutManager(this, COLUMN_COUNT);
    
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    final int viewWidth = (int) (metrics.widthPixels * 0.96);
    final int itemWidth = activity.getResources().getDimensionPixelOffset(R.dimen.movie_recycler_view_width);
    final int leftSpace = (viewWidth - column * itemWidth) / ((column * (column - 1)));
    final int topSpace = DimensionUtil.dp2px(activity, DimensionUtil.DEFAULT_RECYCLER_VIEW_ITEM_SPACE);   
    recyclerView.addItemDecoration(SpaceItemDecoration.newGridDecoration(column, leftSpace, topSpace));
    

    相关文章

      网友评论

          本文标题:RecyclerView的ItemDecoration

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