Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does RecyclerView.ViewHolder always have to be an inner class?

I have two RecyclerView.Adapters that are using exactly the same RecyclerView.ViewHolders as inner classes.

I wanted to get rid of code duplication and made these ViewHolders free, separate class so the brand new class now can be used by any RecyclerView.Adapters.

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.

like image 519
Egemen Hamutçu Avatar asked Sep 06 '16 10:09

Egemen Hamutçu


People also ask

How does RecyclerView ViewHolder work?

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.

What is the relationship between RecyclerView adapter and RecyclerView ViewHolder?

ViewHolder is not bound to an item or the given RecyclerView. Adapter is not part of this Adapter (if this Adapter merges other adapters).

What is ViewHolder in Kotlin?

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.


2 Answers

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.

like image 58
Maciej Sikora Avatar answered Oct 09 '22 12:10

Maciej Sikora


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.

like image 43
Mehraj Malik Avatar answered Oct 09 '22 11:10

Mehraj Malik