RecyclerView GridLayoutManager: how to auto-detect span count?

后端 未结 13 605
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 18:45

Using the new GridLayoutManager: https://developer.android.com/reference/android/support/v7/widget/GridLayoutManager.html

It takes an explicit span count, so the pro

13条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 18:54

    Set spanCount to a large number (which is the max number of column) and set a custom SpanSizeLookup to the GridLayoutManager.

    mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int i) {
            return SPAN_COUNT / (int) (mRecyclerView.getMeasuredWidth()/ CELL_SIZE_IN_PX);
        }
    });
    

    It's a bit ugly, but it work.

    I think a manager like AutoSpanGridLayoutManager would be the best solution, but i didn't find anything like that.

    EDIT : There is a bug, on some device it add blank space to the right

提交回复
热议问题