ListView Smooth Scrolling

Android Tutorial

FP
1 min readJul 27, 2018
Android Development Tutorial

ListView adapter renders each row by calling the method “findViewById”, this reduces considerably the performance and can lead to OutOfMemoryException. ListView adapter keeps references to past views and creating a new one on each scroll. To bypass this issue, android had a RecycleView widget that is an expansion of a ListView, it has the ViewHolder feature by default.

The ViewHolder holds the views and cache it, so Android don’t need to call “findViewById” each time. RecycleView is quite complex to implement. On the other hand, it is possible to implement ViewHolder pattern in ListView that performs the same as using a RecycleView.

Define the ViewHolder inner static class inside the ListView adapter.

In this example, the data is a list of strings, referred as String[], so a TextView will be used to display data for each row of the List.

Create a custum adapter that extends the ArrayAdapter providing the type of the data and override the getView method to implement the ViewHolder.

On the first inflation of the view, it saves the viewHolder reference to the view. Each time it recycles, it gets the viewHolder reference to bind the data.

--

--

No responses yet