2

I have multiple View types in my RecyclerView. I understand I need to write multiple RecyclerView.ViewHolders.

I'm trying to write these in a different package to make the whole project organization cleaner.

I know that while using the ViewHolder pattern for a ListView, the ViewHolders should(must/can?) be made static inner classes.

Can the ViewHolders for RecyclerView and ListViewbe non-static and non-inner and still retain the performance benefit they were intended for?

Veneet Reddy
  • 2,707
  • 1
  • 24
  • 40

1 Answers1

4

Yes. It is best practice to create recyclerview adapter using separate non-static class for RecyclerView.ViewHolder. The benefits are:

  • By creating inner static class and separate , It is reusable in the case of same RecyclerView.ViewHolder for other adapters.

  • If your using RecyclerView.ViewHolder in only single adapter, you can declare it into inner class of your adapter.

Talk about performance :

As per GC performance hit for inner class vs. static nested class and advantage of recyclerview:

While using RecyclerView, it's recycle the instances viewholder, so the memory impact is not a problem. The static one will take less memory than the other one.

Look this and this good answer's to get more idea.

I hope it's help's you.

Community
  • 1
  • 1
pRaNaY
  • 24,642
  • 24
  • 96
  • 146