1

I'm sharing a RecycledViewPool between different RecyclerViews with same view type. Although the view type has different view bounds. For eg: In RecyclerView1 the width of a single item is 100dp where as in RecyclerView2 the width of a single items is 80dp.

Now whenever i scroll through the elements of either of the RecyclerView the RecycledViewPool returns me ViewHolder of a different RecyclerView. So while scrolling in one RecyclerView i get items with width as 100dp as well as 80dp since the view type for both the RecyclerView are same.

Adapter for RecyclerView1:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<HomeData> {
    val viewHolder = super.onCreateViewHolder(parent, viewType)
    val view = viewHolder.itemView
    view.setWidth(100)
    return viewHolder
}

Adapter for RecyclerView2:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<HomeData> {
    val viewHolder = super.onCreateViewHolder(parent, viewType)
    val view = viewHolder.itemView
    view.setWidth(80)
    return viewHolder
}

Both the RecyclerView have the same view type and share the same viewpool here.

Am i missing some configuration while setting the viewpool?

Deep Lathia
  • 750
  • 7
  • 18
  • I have to question why you have a view pool. If the only difference between the two _RecyclerView_ view holder views is the width, you can change the width when the view holders are bound. – Cheticamp Aug 27 '22 at 17:13
  • Width is the one of the most significant difference among other differences. Also i wouldn't want to modify the view bounds while binding the view and do it only while creating it. Sharing a viewpool helps me inflate lesser layouts. – Deep Lathia Aug 27 '22 at 17:16
  • Also, do you think it's possible to have same viewtypes with different view bounds share the same view pool? If yes, how exactly does it map the view holders? – Deep Lathia Aug 27 '22 at 17:18
  • 1
    My question is why share a view pool if the views are, in fact, different? I don't understand what you are trying to do. – Cheticamp Aug 27 '22 at 17:23
  • Because the data that both views are representing is almost the same, just few things like view width, font sizes, image aspect ratio are changing. Otherwise the elements in both the views are same. Do you think i shouldn't be sharing view pool in such a use case? – Deep Lathia Aug 27 '22 at 17:25
  • You can share the view pool if you want, but you may want to give the two type of view different view types so the right one is selected. I would also consider not using the view pool to see if there is any benefit to doing so. – Cheticamp Aug 27 '22 at 17:32
  • Since you are making changes to the ViewHolder & returning it in the `onCreateViewHolder`, it persists in the ViewPool (afaik). This makes the view actually `different` as what @Cheticamp said above & therefore a new `ViewHolder` is not requested if one exists in the pool. You should use the `RecycledViewPool` when the layout is completely same. Alternatively, you could change the `height/width` in `onBindViewHolder`. – Darshan Aug 27 '22 at 18:21
  • Are both these `RecyclerViews` on-screen at the same time? If so I feel like you're not actually getting any benefit, the pool still needs to create enough `ViewHolder`s (and inflate their view hiearchies) to fill each display (plus a few extra for the items just out of view) - it's the same amount as if they just handled their own. And honestly, even if it did reduce the number, I'm not sure how much benefit you'd get. You'll be getting a performance impact from having to recalculate the layouts. If the *data* is the same, you might just want to reuse your *`Adapter`*, not the `ViewHolder`s! – cactustictacs Aug 27 '22 at 23:38

0 Answers0