RecyclerView GridLayoutManager with full width header

痞子三分冷 提交于 2019-12-31 09:15:39

问题


I'm using a very helpful example here to show a RecyclerView and a GridLayoutManager to show a grid with a header.

It looks pretty good, but my graphic designer wants the header item to take up the full width of the RecyclerView. Right now there is padding.

When I set up the GridLayoutManager I add in padding (which I still want for the other grid items): [it's using Xamarin C#]

var numColumns = myListView.MeasuredWidth / tileSizeMax;
myGridView.SetPadding(tilePadding, tilePadding, tilePadding, tilePadding);
layoutManager = new GridLayoutManager(Activity, numColumns);
myListView.SetLayoutManager(layoutManager);

So, how can I set the padding to be different for the header item...or make it draw itself over the padding?


回答1:


Try with this:

mLayoutManager = new GridLayoutManager(this, 2);
        mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(mAdapter.getItemViewType(position)){
                    case MyAdapter.HEADER:
                        return 2;

                    case MyAdapter.ITEM:
                    default:
                        return 1;
                }
            }
        });

And check these links:

http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html Set span for items in GridLayoutManager using SpanSizeLookup



来源:https://stackoverflow.com/questions/37642869/recyclerview-gridlayoutmanager-with-full-width-header

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