When is onCreateViewHolder called and why? RecyclerView

Viewed 1734

Up to this point I thought that onCreateViewHolder in RecyclerView.Adapter was called when I was calling adapter's constructor with adapter = new RecyclerViewAdapter(this, list), but I realised that it is actually called at the end of Activity's onCreate method after all the code is executed, is the reason for that have something to do with that the RecyclerView doesn't create unnecesary views, and creates only how many can fit on the device's screen?

1 Answers

It's called when RecyclerView.LayoutManager determines which and how many views will be visible within RecyclerView, and RecyclerView.RecycledViewPool does not contain enough ViewHolders to re-use.

This usually happens during each layout phase and on (first) scroll.

Related