luizgrp / SectionedRecyclerViewAdapter

An Adapter that allows a RecyclerView to be split into Sections with headers and/or footers. Each Section can have its state controlled individually.
MIT License
1.68k stars 372 forks source link

Optimization for sticky header implementation #160

Closed yccheok closed 5 years ago

yccheok commented 5 years ago

I notice there are quite a number of request regarding sticky header.

https://github.com/luizgrp/SectionedRecyclerViewAdapter/issues/7 https://github.com/luizgrp/SectionedRecyclerViewAdapter/issues/131 https://github.com/luizgrp/SectionedRecyclerViewAdapter/issues/78

So far, the solution which I think is less "intrusive" so far, is the one described in https://stackoverflow.com/a/44327350/72437

The key idea, is to draw an overlay header view, in RecyclerView.ItemDecoration's onDrawOver, on the top of existing solution.

However, I notice there might be a potential performance problem. A new view will inflate each time (in getHeaderViewForItem), before drawing.

I was wondering, how can we have a "recycle-able" overlay view, so that the overlay view can be re-use again and again?

Thank you

luizgrp commented 5 years ago

Hi @yccheok , I think this feature shouldn't be in this library, as this library is a custom adapter and not a custom view.

But I'm happy if anyone implement a separate library for sticky headers on top of this library, I would add a reference to it on the README.

This library gives freedom for the developers to have different header layouts for different sections, so it will have to inflate the header views dynamically and have the potential performance problem you mentioned. If your sections have different header layouts, this problem already exists, as the RecyclerView would have to inflate it every time when scrolling.

One solution is to make it simple and have only one header layout for the sticky headers, and ignore the header layout of the section. Thus you would only inflate the sticky header view once but that will not be as flexible as the adapter is.

So if you are really worried about performance, you would have to modify the onCreateViewHolder method of SectionedRecyclerViewAdapter class to store in some structure the header views that were inflated so you can share it with the sticky header code. You would have to take care of clearing that structure too.

Another solution to explore is to see if you can use the RecyclerViewPool, there is a method to get a view by view type.