2

I can't understand how the RecyclerView recycle the views and reuses them if there are more than one type of views.I know that the Adapter will create two more ViewHolders for reusing.In that case,there are no more than two type of views that can be reused to present the next item in the window.But if the type of next item doesn't match either of the two types that have been recycled,there will be no views available to be reused to present the next item because the type of view it needs is still visible on the screen and cannot not be recycled.How does the system handle it ? Any and all answers are appreciated.

lwenkun
  • 31
  • 1
  • 8

3 Answers3

2

If there is a recycled view available that MATCHES to the new one that's becoming visible, then it will re-use. Otherwise it'll create the new object for the new view. It uses getViewType () to find that.
You can take a look at this for few more related points.

Community
  • 1
  • 1
cgr
  • 4,578
  • 2
  • 28
  • 52
  • 1
    Welcome. Vote and Accept any answer. That's the energy for answer givers. ;-) – cgr Nov 25 '15 at 18:27
0

The RecyclerView will create as many ViewHolders as it needs to fill the available visible space. Recycling happens when one scrolls out of view. At that time, it will be reused for a new item scrolling into view.

RussHWolf
  • 3,555
  • 1
  • 19
  • 26
  • But what if the type of view recycled doesn't match the type of new item scrolling into view?Can the view recycled be reused for the new? – lwenkun Nov 25 '15 at 17:15
  • No, only an item of the correct viewType will be reused. The `ViewHolder` will be reused the next time an item of the same type is needed. – RussHWolf Nov 25 '15 at 17:47
0

The number of ViewHolders that have been created. Typically, you can figure out how many there should be by determining how many list items fit on your screen at once and add 2 to 4 to that number. That isn't the exact formula, but will give you an idea of how many ViewHolders have been created to display any given RecyclerView.