Don't recycle specific view type in RecyclerView
Asked Answered
F

2

12

I have a recycler view which is reusing view, my problem is that one viewholder contains a ViewPager and, if the recyclerview's adapter reuse the view where the viewholder is inflated then an exception occurs on the viewpager. However, the onCreateViewHolder method is only called the first time. How could I avoid this behaviour and achieve that the onCreateViewHolder can be called more than once?

Thanks in advance

Fletafletch answered 9/11, 2016 at 15:19 Comment(3)
Avoid reusing item on recyclerview == Avoid recycling of view in RecyclerView ... so why using RecyclerView at all?Buller
And now seriously ... why don't you come back here with minimal example of your code and exception that you are facing ? Isn't better to use RecyclerView and fix an error?Buller
@Buller there is still a usefulness of using recyclerview even though the views are not recycled, mainly that the views are only created as many as needed on the initial load.Crevasse
O
20

use recyclerView.getRecycledViewPool().setMaxRecycledViews(YOUR_VIEW_TYPE, 0); This will disable recycling of the specified view type

Occasionalism answered 9/11, 2016 at 15:22 Comment(0)
A
0

This is what the getItemViewType override is for.

If you override getItemViewType to return a different value for the ViewPager position, the RecyclerView will use that to know when it should recycle the view.

Acupuncture answered 9/11, 2016 at 15:23 Comment(2)
I'm already using it, my problem is that this is the same type on the same position. The issue is happening when I scroll down and then up again (so the same viewholder is shown again)Fletafletch
Then you really need to post your code and your logcat, this shouldn't be too hard to fix.Acupuncture

© 2022 - 2024 — McMap. All rights reserved.