简体   繁体   English

GridLayoutManager 中的可变列数

[英]Variable number of columns in GridLayoutManager

I wanted to display variable number of columns in a row of GridLayoutManager while using RecyclerView.我想在使用 RecyclerView 时在 GridLayoutManager 的一行中显示可变数量的列。 The number of columns to be displayed depends on the size of the column's TextView.要显示的列数取决于列的 TextView 的大小。

I don't know the column width as the text is being dynamically put in it.我不知道列宽,因为文本是动态放入的。

Can anyone help?任何人都可以帮忙吗? StaggeredGridLayoutManager is not solving my purpose as it customizes the height but takes fixed number of columns. StaggeredGridLayoutManager 并没有解决我的目的,因为它自定义了高度但采用了固定数量的列。

Take a look at the setSpanSizeLookup method of the GridLayoutManager . 看一下GridLayoutManagersetSpanSizeLookup方法。 It lets you specify the span size for specific positions of your RecyclerView . 它允许您指定RecyclerView特定位置的跨度大小。 So maybe you could use it to fit with your requirements for the variable column number. 所以也许您可以使用它来满足您对变量列号的要求。

Edit: 编辑:

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
        }
    }
});

When using this sort of layout manager your RecyclerView should look like this: 使用这种布局manager您的RecyclerView应如下所示:

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

(only boxes with numbers represent items of your RecyclerView , other boxes are just empty spaces) (只有带数字的方框表示RecyclerView项目,其他方框只是空格)

If you want to make a variation like: 4 columns, 5 columns, 6 columns... You can get the MMC (minimum multiple common) between this numbers (60) and set the GridLayoutManager: 如果你想做一个变化,如:4列,5列,6列......你可以得到这些数字之间的MMC(最小多个共同点)(60)并设置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
        }
    });
Then you could return 10, 12 or 15 for 6, 5 and 4 columns on getSpanSize() 然后你可以在getSpanSize()上为6,5和4列返回10,12或15

You can use the span based on calculating the width.您可以根据计算宽度来使用跨度。

     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);
                    }
                }

For calculating the columns you can use this method:要计算列,您可以使用此方法:

        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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM