【问题标题】:Divide width between two columns in GridLayoutManager在 GridLayoutManager 中的两列之间划分宽度
【发布时间】:2017-12-21 07:46:40
【问题描述】:

我使用GridLayoutManager 作为我的布局管理器,用于回收站视图。

  mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));

结果如下:

但我想在两列之间平均分配宽度。我该怎么做?

【问题讨论】:

    标签: android android-layout android-recyclerview gridlayoutmanager


    【解决方案1】:

    尝试将其添加到您的 recyclerView

    mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                int position = parent.getChildAdapterPosition(view); // item position
                int spanCount = 2;
                int spacing = 10;//spacing between views in grid
    
                if (position >= 0) {
                    int column = position % spanCount; // item column
    
                    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 = 0;
                    outRect.right = 0;
                    outRect.top = 0;
                    outRect.bottom = 0;
                }
            }
        });
    

    【讨论】:

      【解决方案2】:

      像你一样设置列数:

      mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
      

      ...实际上使网格分成相同宽度的列。

      这表明问题出在其他地方,可能在项目布局。我想某些空格(paddings/margins)是造成项目向右移动的原因。尝试删除这些间距并检查它是否有效。

      如果有帮助,您可以随时使用 ItemDecoration 添加新间距,但这不是列大小不正确的原因。

      我认为开启Show Layout Bounds option 可能对您有所帮助。

      【讨论】:

        【解决方案3】:

        您可以使用自定义 ItemDecoration 并且可以根据需要拆分空间:

        SpacesItemDecoration 可以帮助您:

        public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
            private int space;
        
            public SpacesItemDecoration(int space) {
                this.space = space;
            }
        
            @Override
            public void getItemOffsets(Rect outRect, View view,
                                       RecyclerView parent, RecyclerView.State state) {
        
                outRect.bottom = space;
        
                // Add top margin only for the first item to avoid double space between items
                if (parent.getChildLayoutPosition(view)%2 == 0) {
                    outRect.left = space;
                    outRect.right = space;
                } else {
                    outRect.right = space;
                }
            }
        }
        

        在您的活动或片段中:

        int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.spacing);
                spacesItemDecoration = new SpacesItemDecoration(spacingInPixels);
        
              recyclerview.addItemDecoration(spacesItemDecoration);
        

        您可以根据需要更改 outRect.left 和 outRect.Right

        【讨论】:

        • 是的,但如果你能看到它会有一些变化
        【解决方案4】:

        您可以为此使用ItemDecoration,看看here

        还有一些很好的答案here also

        【讨论】:

          【解决方案5】:

          我为等间距写了一个library,它支持垂直/水平、LTR/RTL、LinearLayout/GridLayout 管理器和边缘包含。它基本上是一个文件,因此您可以将该文件复制粘贴到您的代码中。

          我尝试支持StaggeredGridLayout,但此布局返回的跨度索引不可靠。我很高兴听到任何建议。

          【讨论】:

            猜你喜欢
            • 2014-04-06
            • 1970-01-01
            • 2011-04-28
            • 2011-10-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-01-08
            相关资源
            最近更新 更多