bgogetap / StickyHeaders

Easily add Sticky Headers to your RecyclerView
Apache License 2.0
521 stars 88 forks source link

New header instance is not created #55

Closed seikomg closed 7 years ago

seikomg commented 7 years ago

Hello,

Thank you for a very convenient library. The sample app works just fine, but when I try to incorporate the library in my app, I see 'java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first'. I do not attach my view to the parent when inflating the xml in onCreateViewHolder(). The recycler view works perfectly fine without the library. Any ideas? Also, in StickyHeaderPositioner.java, I see this.currentHeader = currentViewHolder.itemView;. I am curious to know how and when a new instance is created.

bgogetap commented 7 years ago

When a new instance of the ViewHolder is created? Check RecyclerViewRetriever#getViewHolderForPosition(int). This calls into your adapter to generate a view holder when needed.

I cannot help with the IllegalStateException without seeing how views in your view holder are accessed, but I'd check into any calls to retrieve the itemView of your view holder in your code (whether through accessing the itemView property of the view holder superclass, or possibly through a field you define in your view holder class).

However, I would think any outside interference with the itemView (adding it to a different parent besides the RecyclerView which the layout manager will handle) would cause similar errors without the library, which you say does not happen. Are you maybe using any other libraries that deal with RecyclerView?

seikomg commented 7 years ago

Thanks for the quick response. So it turned out that we were holding references to the view holders and creating new ones only when the reference is null. Everything works well once I remove those null checks! I did have another quick question. Can I explicitly detach the header based on which item is being displayed in the recycler view? Currently, it only detaches itself when you scroll up so that the header shows up as a part of the recycler view, but not if you scroll all the way down. I want the header to detach when I scroll down past a particular view.

bgogetap commented 7 years ago

The only way to control when a header is sticky is by its position in the data set.

You could have an "invisible" header that pushes it off. Or, you could remove the item from your data set by doing some logic in a scroll listener or something. I realize neither of those two options are ideal, but currently that would be the only way to do it.

I may look into supporting what you need, but I'll need to do some research on what kind of changes would be needed.