Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What difference between static and non static viewholder in RecyclerView Adapter?

What are the advantages of this approach (using static nested class in my class MyAdapter extends RecyclerView.Adapter):

static class MyVH extends RecyclerView.ViewHolder {...} 

And this approach (using member inner class):

  class MyVH extends RecyclerView.ViewHolder {...} 

Or it doesn't affect performance and both approaches could be used?

like image 849
Lester Avatar asked Jul 08 '15 19:07

Lester


People also ask

What is the role of the ViewHolder in a RecyclerView?

A ViewHolder describes an item view and metadata about its place within the RecyclerView. RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive View.

How does a 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 viewType in onCreateViewHolder?

onCreateViewHolder(parent: ViewGroup, viewType: Int) Parent is the ViewGroup into which the new View will be added after it is bound to an adapter position and viewType is the type of the new View. This method will return a new ViewHolder that holds a View of the given view type.

What is onCreateViewHolder?

onCreateViewHolder only creates a new view holder when there are no existing view holders which the RecyclerView can reuse. So, for instance, if your RecyclerView can display 5 items at a time, it will create 5-6 ViewHolders , and then automatically reuse them, each time calling onBindViewHolder .


2 Answers

It is more a java question than an Android question. It is recommended to use static for inner classes to avoid memory leaks if you will take their instances out of the class. You can have a look at this awesome post that explains the memory leaks on inner classes.

Basically what nyx says:

  • If you declare the viewholder as static you can reuse it in other adapters. Anyway, I do not recommend to do it, create a new separated class and use it from multiple places, it does make more sense. One class for one purpose.
  • In the case of view holders, this classes will be only used inside the adapter, their instances should not go to the fragment or activity or elsewhere just by definition. This means having it static or non-static, in the case of view holders, is the same.

Answering your performance question, you can have a look at this answer. The static one will take less memory than the other one, but again, we are talking about recyclers which will recycle the instances, so the memory impact is not a problem.

like image 192
droidpl Avatar answered Oct 12 '22 17:10

droidpl


By using static it just means you can re-use MyVh in other adapters. If you know for certain that you'll only need MyVh in that one adapter, then you should make it non-static.

If you will need it in other adapters it may even be better to just create it as a separate class entirely, rather than a nested class.

There should be no effects on performance for static vs non-static!

like image 25
Nyx Avatar answered Oct 12 '22 17:10

Nyx