I can't fully understand getItemViewType(), I searched on the internet and every search tells how to use it but no one is telling how it works. I want to know why it is used, how it works and if it comes before on create view holder and any help will be appreciated.
I searched on the internet I expected to find full explanation but I found its working.
CodePudding user response:
A RecyclerView can show multiple types of items in it. For example, it can show images and videos in one recycler. These obviously would have different views to display them. This function returns the type of view this item should use to display this item. The type doesn't actually mean anything, it's just used by the recycler to pick the right type of view when it recycles them. When it calls onCreateView, it remembers the type of this item was a 1, and saves it in a pool of views for that type. When it later needs to recycle another item of type 1, it looks in that pool to see if it already has a view, and reuses it if so.
You can number them whatever you want, although using an enum and consecutive integers is generally the easiest and most maintainable path.
If your recycler view only needs to display a single type of view, its ok to just hard code this to always return 1.