簡體   English   中英

GridLayoutManager 中的可變列數

[英]Variable number of columns in GridLayoutManager

我想在使用 RecyclerView 時在 GridLayoutManager 的一行中顯示可變數量的列。 要顯示的列數取決於列的 TextView 的大小。

我不知道列寬,因為文本是動態放入的。

任何人都可以幫忙嗎? StaggeredGridLayoutManager 並沒有解決我的目的,因為它自定義了高度但采用了固定數量的列。

看一下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項目,其他方框只是空格)

如果你想做一個變化,如: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
        }
    });
然后你可以在getSpanSize()上為6,5和4列返回10,12或15

您可以根據計算寬度來使用跨度。

     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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM