【问题标题】:Variable number of columns in GridLayoutManagerGridLayoutManager 中的可变列数
【发布时间】:2015-12-22 04:18:41
【问题描述】:

我想在使用 RecyclerView 时在 GridLayoutManager 的一行中显示可变数量的列。要显示的列数取决于列的 TextView 的大小。

我不知道列宽,因为文本是动态放入其中的。

有人可以帮忙吗? StaggeredGridLayoutManager 没有解决我的目的,因为它自定义了高度但采用了固定数量的列。

【问题讨论】:

    标签: android android-recyclerview android-gridlayout staggeredgridlayout


    【解决方案1】:

    您可以根据计算宽度使用跨度。

         public class AutoFitGridLayoutManager extends GridLayoutManager {
                        private boolean columnWidthChanged = true;
                        Context context;
    
                        public AutoFitGridLayoutManager(Context context) {
                            super(context, 1);
                            this.context = context;
                            setColumnWidth();
                        }
    
                        public void setColumnWidth() {
                                columnWidthChanged = true;
                        }
    
                        @Override
                        public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
                            if (columnWidthChanged) {
                              
                                //int spanCount = Math.max(1, totalSpace / columnWidth);
                                //setSpanCount(spanCount);
                                setSpanCount(Utils.calculateNoOfColumns(context));
                                columnWidthChanged = false;
                            }
                            super.onLayoutChildren(recycler, state);
                        }
                    }
    

    对于计算列,您可以使用此方法:

            public static int calculateNoOfColumns(Context context) {
    
                    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
                    float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
                    int scalingFactor = 200; // You can vary the value held by the scalingFactor
                    // variable. The smaller it is the more no. of columns you can display, and the
                    // larger the value the less no. of columns will be calculated. It is the scaling
                    // factor to tweak to your needs.
                    int columnCount = (int) (dpWidth / scalingFactor);
                    return (columnCount>=2?columnCount:2); // if column no. is less than 2, we still display 2 columns
    }
    

    【讨论】:

      【解决方案2】:

      如果您想做出如下变化:4 列、5 列、6 列... 您可以获取此数字 (60) 之间的 MMC(最小公用数)并设置 GridLayoutManager:

      
          GridLayoutManager manager = new GridLayoutManager(context, 60); // set the grid with the MMC
          manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
              @Override
              public int getSpanSize(int position) {
                  return 12; // 60/12 = 5 Columns
              }
          });
      
      然后您可以在 getSpanSize() 上为 6、5 和 4 列返回 10、12 或 15

      【讨论】:

        【解决方案3】:

        看看GridLayoutManagersetSpanSizeLookup 方法。它允许您为RecyclerView 的特定位置指定跨度大小。因此,也许您可​​以使用它来满足您对可变列号的要求。

        编辑:

        GridLayoutManager manager = new GridLayoutManager(context, 2); // MAX NUMBER OF SPACES
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (position == 1 || position == 6) {
                    return 2; // ITEMS AT POSITION 1 AND 6 OCCUPY 2 SPACES
                } else {
                    return 1; // OTHER ITEMS OCCUPY ONLY A SINGLE SPACE
                }
            }
        });
        

        当使用这种布局 manager 你的 RecyclerView 应该是这样的:

        +---+---+
        | 0 |   |
        +---+---+
        |   1   |
        +---+---+
        | 2 | 3 |
        +---+---+
        | 4 | 5 |
        +---+---+
        |   6   |
        +---+---+
        

        (只有带数字的方框代表你RecyclerView的项目,其他方框只是空格)

        【讨论】:

        • 是的,我做到了,但无法理解它的实现。你能发一个例子吗/
        • 我在答案中添加了一个示例
        • 非常好的解释先生
        猜你喜欢
        • 2015-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-17
        • 2018-07-28
        • 1970-01-01
        相关资源
        最近更新 更多