Dynamically change the number of columns of a GridLayoutManager

后端 未结 3 1970
粉色の甜心
粉色の甜心 2021-02-07 04:06

I am actually using a GridLayoutManager with two columns in my app and I would like to have one column depending on the view type used.

Here is the code I have in the me

3条回答
  •  盖世英雄少女心
    2021-02-07 04:42

    mGridLayoutManager = new GridLayoutManager(mContext, 2);
    mGridLayoutManager.setSpanSizeLookup(onSpanSizeLookup);    
    
    /**
     * Helper class to set span size for grid items based on orientation and device type
     */
    GridLayoutManager.SpanSizeLookup onSpanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            return mHomeListAdapter.getItemViewType(position) == TYPE_PREMIUM ? 2 : 1;
        }
    };
    

提交回复
热议问题