Home » Android » android – RecyclerView detaches view still visible

android – RecyclerView detaches view still visible

Posted by: admin May 14, 2020 Leave a comment

Questions:

Current scenario

I’m using this library https://github.com/kanytu/android-parallax-recyclerview to achieve a parallax effect on a RecyclerView. So far, so good. However I would like get an effect like Google Newsstand where the header is still bellow the cards.

Problem

The problem is that RecyclerView (or LinerLayoutManager) detaches the header view the moment the first element of the list touches the top of the parent view:

enter image description here

As you can see the moment the first cardview touches the top is the moment RecyclerView detaches the header.

I’m sure that there is no problem on the logic itself I get RecyclerView.findViewHolderForPosition(0) == null when the card reaches the top. Proving that the header is recycled.

Tried Solutions

I tried many things like:

ViewHolder.setIsRecyclable using this method to set the holder to not recycler doesn’t do any effect.

LayoutManager.ignoreView I tried marking the view to be ignored from scapping and recycling. The result was an exception saying:
Trying to recycle an ignored view holder. You should first call stopIgnoringView(view) before calling recycle.

setItemViewCacheSize Doesn’t do anything. Tried calling setItemViewCacheSize(50) and it doesn’t do anything to the header.

setMaxRecycledViews Tried setting the max of recycled views with viewType=HEADER to 0 and it still recycles it.

Conclusion

So I question if there is anyway to mark the item for not getting detached until I order so (like checking if it’s still visible and then detach it).

There is also an issue on the github about it: https://github.com/kanytu/android-parallax-recyclerview/issues/7

How to&Answers:

You can use the new design support library’s CoordinatorLayout with your recyclerview and make the same effect with no issues like this.

check this link: http://android-developers.blogspot.com/2015/05/android-design-support-library.html