I have two RecyclerView.Adapter
s that are using exactly the same RecyclerView.ViewHolder
s as inner classes.
I wanted to get rid of code duplication and made these ViewHolder
s free, separate class so the brand new class now can be used by any RecyclerView.Adapter
s.
However I faced lots of troubles for example difficulty in accessing the adapter objects. getAdapterPosition()
always returns -1
.
So I changed my mind and made a super RecyclerView.Adapter
class which is extended by those adapters and put the ViewHolder in the superclass so those adapters can use it from subclass.
But I want to know if ViewHolder
does have to be an inner class. This makes me annoyed. Please do NOT advice me to combine the Adapter classes, they are completely different as the ViewHolder
is just a special viewType
that can be appear in any RecyclerView
I am waiting for your better approaches which make me feel better.
Regards.
ViewHolder: ViewHolder is a type of helper class that allows us to draw the UI for individual items on the screen. LayoutManager: LayoutManager in recyclerView assists us in determining how to display the items on the screen. It can be done either linearly or in a grid.
ViewHolder is not bound to an item or the given RecyclerView. Adapter is not part of this Adapter (if this Adapter merges other adapters).
ViewHolder class which caches views associated with the default Preference layouts. A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive View#findViewById(int) results.
ViewHolder can be outside class. Inner class is only a proposition in all tutorials for RecyclerView, it is a better way if your ViewHolder should have access to all Adapter parameters, even those private, but any access or objects relations can be recreated by access methods in Adapter and ViewHolder.
I have created stand alone project with usage of ViewHolder as an outside class, take a look. Link to repository - https://github.com/maciejsikora/outsideviewholder.
I think also the cause of your problem is the fact that in the first code version ViewHolder was an inner class and had access to the properties, after change into an outside class, the code should have been refactored, and in the result all relations between ViewHolder and Adapter should be deeply checked.
Answer for the question is - ViewHolder doesn't have to be inner class, and your problems are caused by invalid code implementation in using ViewHolder as an outside class.
Actually, No.
First you need to understand that why we need Inner class?
We do need Inner classes where we want that only particular class will have this functionality.Like we have many inner class for many Listeners
and Button
onClick and many more.
So we use inner class for making things private,short and simple .
You can make this thing(ViewHolder
) a separate class.But that will not be efficient,clear(If you make another class it will add an extra class to your project),and effective way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With