GridView: Adding custom row every 20 items

≡放荡痞女 提交于 2019-12-13 21:25:57

问题


I have a standard GridView displaying images. I would like for every 20 items/images that are shown, a custom layout to be shown.

Here is an good example of what I mean:

Would anyone know how to achieve this? any advice is appreciated. Thank you.


回答1:


Would anyone know how to achieve this?

I did it. You can achieve this easily by using RecyclerView and GridLayoutManager and 2 view types in RecyclerView.Adapter. You can see codes below: With type TYPE_ADS, you span colum by 2. With normal item TYPE_ITEM, you don't span colum (means value = 1)

mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
GridLayoutManager glm = new GridLayoutManager(this, 2);
glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(mAdapter.getItemViewType(position)){
                    case MyAdapter.TYPE_ADS:
                        return 2;
                    case MyAdapter.TYPE_ITEM:
                        return 1;
                    default:
                        return -1;
                }
            }
        });

mRecyclerView.setLayoutManager(glm); 
mRecyclerView.setAdapter(mAdapter);


来源:https://stackoverflow.com/questions/41570942/gridview-adding-custom-row-every-20-items

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