GridLayoutManager中

时间:2015-09-24 14:03:15

标签: android android-recyclerview android-gridlayout staggeredgridlayout

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

我不知道文字正在动态放入其中的列宽。

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

3 个答案:

答案 0 :(得分:25)

查看GridLayoutManager的{​​{3}}方法。它允许您指定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的项目,其他方框只是空格)

答案 1 :(得分:4)

如果你想做一个变化,如: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

答案 2 :(得分:0)

您可以基于计算宽度来使用跨度。

     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
}