美文网首页Android-recyclerview
RecycleView嵌套RecycleView问题集

RecycleView嵌套RecycleView问题集

作者: jxtx | 来源:发表于2020-09-30 16:33 被阅读0次

    1.问题:子RecycleView网格布局的话,使用GridSpacingItemDecoration控制间隔大小,不断上下滑动,部分间距会不断变大,超出界面
    原因:未知
    解决方法:
    方法1.亲测有效

     if (holder.rlv_select.getItemDecorationCount() == 0) {
                GridSpacingItemDecoration gridSpacingItemDecoration=new GridSpacingItemDecoration(4, DisplayUtils.dp2px(context,33), DisplayUtils.dp2px(context,10),false);
                holder.rlv_select.addItemDecoration(gridSpacingItemDecoration);
            }
    

    方法2.

    recyclerView.removeItemDecoration();
    
    
    public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
    
        private int spanCount;
        private int spacing;
        private int topSpaceing;
        private boolean includeEdge;
    
        public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
            this.spanCount = spanCount;
            this.spacing = spacing;
            this.includeEdge = includeEdge;
        }
    
        public GridSpacingItemDecoration(int spanCount, int spacing,int topSpaceing, boolean includeEdge) {
            this.spanCount = spanCount;
            this.spacing = spacing;
            this.includeEdge = includeEdge;
            this.topSpaceing = topSpaceing;
        }
    
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int position = parent.getChildAdapterPosition(view); // item position
            int column = position % spanCount; // item column
    
            if (includeEdge) {
                outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
                outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
    
                if (position < spanCount) { // top edge
                    outRect.top = spacing;
                }
                outRect.bottom = spacing; // item bottom
            } else {
                outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
                outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f /    spanCount) * spacing)
                if (position >= spanCount) {
                    outRect.top = topSpaceing; // item top
                }
            }
        }
    }
    
    
    

    参考:RecyclerView刷新ItemDecorations间距不断增加问题的处理

    相关文章

      网友评论

        本文标题:RecycleView嵌套RecycleView问题集

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