Reputation: 77
In RecyclerView, the method onCreateViewHolder will create new view holders if need be. I know that It will be invoked as many times as there are items in your list that can fit on the screen. But, for some odd reason, when I scroll down my RecyclableList, `onCreateViewHolder gets invoked a few more times. Why does it create a new viewHolder? in what cases will viewHolder be created? I thought it only needs to be invoked once.
logcat:
09-27 16:39:20.805 8603-8603/teamtreehouse.com.myapplication D/testHourViewHolder: I AM INVOKED
09-27 16:39:20.814 8603-8603/teamtreehouse.com.myapplication D/testHourViewHolder: I AM INVOKED
09-27 16:39:20.817 8603-8603/teamtreehouse.com.myapplication D/testHourViewHolder: I AM INVOKED
09-27 16:39:20.818 8603-8603/teamtreehouse.com.myapplication D/testHourViewHolder: I AM INVOKED
09-27 16:39:20.822 8603-8603/teamtreehouse.com.myapplication D/testHourViewHolder: I AM INVOKED
09-27 16:39:20.825 8603-8603/teamtreehouse.com.myapplication D/testHourViewHolder: I AM INVOKED
09-27 16:39:20.826 8603-8603/teamtreehouse.com.myapplication D/testHourViewHolder: I AM INVOKED
these gets invoked when I scroll down:
09-27 16:39:20.827 8603-8603/teamtreehouse.com.myapplication D/testHourViewHolder: I AM INVOKED
09-27 16:39:20.828 8603-8603/teamtreehouse.com.myapplication D/testHourViewHolder: I AM INVOKED
09-27 16:39:20.830 8603-8603/teamtreehouse.com.myapplication D/testHourViewHolder: I AM INVOKED
Upvotes: 0
Views: 136
Reputation: 801
The point of a RecyclerView is that it doesn't need to hold your entire list of objects in memory at all times; this means it does not create ViewHolders for your list items until they are (almost) in view of the screen. So, when you first launch your activity it is creating all of the ViewHolders within view (with a slight threshold to prevent stuttering) and nothing else, then when you scroll it begins generating the views below.
Upvotes: 1