Recycling Views

Figure 9.11 shows 11 rows of Views. You can swipe to scroll through 100 Views to see all of your Crimes. Does that mean that you have 100 View objects in memory? Thanks to your RecyclerView, no.

Creating a View for every item in the list all at once could easily become unworkable. As you can imagine, a list can have far more than 100 items, and your list items can be much more involved than your simple implementation here. Also, a Crime only needs a View when it is onscreen, so there is no need to have 100 Views ready and waiting. It would make far more sense to create view objects only as you need them.

RecyclerView does just that. Instead of creating 100 Views, it creates just enough to fill the screen. When a view is scrolled off the screen, RecyclerView reuses it rather than throwing it away. In short, it lives up to its name: It recycles views over and over.

Because of this, onCreateViewHolder(ViewGroup, Int) will happen a lot less often than onBindViewHolder(ViewHolder, Int). Once enough ViewHolders have been created, RecyclerView stops calling onCreateViewHolder(…). Instead, it saves time and memory by recycling old ViewHolders and passing those into onBindViewHolder(ViewHolder, Int).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.22.136