Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Android Fragments Introducing Fragments Handling Clicks

ViewHolder class

In listView case we declare a static Viewholder class ??why?? But in case of recyclerView we didn't?? why

2 Answers

Joe Brown
Joe Brown
21,465 Points

When the ListView was the main way to display items in a List it was found that using the ViewHolder pattern made the scrolling more efficient and reduced flickering and the like. When they added RecyclerView they knew this so they added this pattern right into it, so it is a part of RecyclerView instead of having to be added on the way it was when using a Listview

no actually i want to understand why private static viewHolder class{} in listView But.. private ViewHolder class{} in RecyclerView

Means why we declare viewHolder a static class in listView but not in RecyclerView

Joe Brown
Joe Brown
21,465 Points

Oh I see what you mean. The example on the Android dev site shows the inner class as static https://developer.android.com/training/material/lists-cards.html As I understand in Java you can only put the static keyword on an inner class. By putting the static keyword on the the inner class it removes that implicit reference you had to the outer containing class. So I would assume it is just a design decision, encapsulate things properly and the like