StaggeredGridLayoutManager. How to have one item in first row and 2 item other rows

拥有回忆 提交于 2019-12-14 04:00:22

问题


I am using recyclerview with staggeredGridLayoutManager. What i am trying to do is to show one header item in first row. then i want to show 2 items in each below row. But i am unable to do so.

Here is code that i used.

    recyclerView = (RecyclerView)findViewById(R.id.recycler);
    staggeredGridLayoutManager = new StaggeredGridLayoutManager(1,1);
    recyclerView.setLayoutManager(staggeredGridLayoutManager);

And these are the results

Help me with this. thanks


回答1:


You could use GridLayoutManager.

GridLayoutManager manager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);

manager.setSpanSizeLookup(
    new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
             // 2 column size for first row
             return (position == 0 ? 2 : 1);
        }
    });

Here we are creating a GridLayoutManager with 2 grid columns. Then only for the first row, we are setting the span size to 2.



来源:https://stackoverflow.com/questions/37042451/staggeredgridlayoutmanager-how-to-have-one-item-in-first-row-and-2-item-other-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!