What if, if you passed a list of 100,000 elements into a RecyclerView

Yusuf Gültaç
4 min readDec 3, 2023

When it comes to presenting large datasets in Android applications, the RecyclerView proves to be a game-changer. In this article, I dive headfirst into the complexities of how RecyclerView tackles the challenge of displaying a list containing 100,000 elements even longer than your Monday to-do list :)

Think of your RecyclerView as the backstage manager in charge of a massive theater production, and the items in your list are like soldiers ready to take the stage. In this theather game, imagine you have a battalion of soldiers, each needing their own look. Now, when a new soldier is about to make an entarance (a new item in your RecyclerView), instead of rushing to sew a new costume, RecyclerView looks into its costume storage. This storage is filled with outfits the soldiers have worn before in previous acts. When a new soldier (item) needs to step into the spotlight, RecyclerView picks a costume from the closet, and voila! The show goes on without the hassle of creating new outfits for each soldier every time they make an appearance. In traditional approach would involve creating a new view for each item in the list. Image this as a creating an entirely new outfit for each perfomer even most of them are playing in same role.

As you may have anticipated by now, the RecyclerView is ingeniously designed not to load all the items at once, regardless of how extensively you scroll. Instead, it cleverly recycles existing views and generates new ones as needed, ensuring an efficient and seamless performance. Picture it as a choreographed theather game where the RecyclerView orchestrates the appearance of each item, effortlessly reusing old costumes or crafting new ones as the dynamic performance unfolds.

// Tested code example that app did not meet any issue.
val dataset = (1..100000).map { Item("Item $it") }

val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
val adapter = MyAdapter(dataset)
recyclerView.adapter = adapter

Of course the stage is set not only with soldiers but also with three key supporters that bring the production to life: the LayoutManager, the RecyclerView.Adapter, and the RecyclerView.ViewHolder.

LayoutManager

Image the LayoutManager as the choreographer. In our theater, it’s responsible for orchestrating the perfect arrangement of soldiers on stage. Every soldier (item) knows their position, creating a visually captivating performance under the LayoutManager’s direction.

RecyclerView.Adapter

Enter the RecyclerView.Adapter, the scriptwriter of theater production. This key player is responsible for managing the storyline (data) that unfolds on stage. The script (data) is in the hands of the RecyclerView.Adapter, shaping the narrative as the play progresses.

RecyclerView.ViewHolder

Now, meet the RecyclerView.ViewHolder, the costume director in the theater. Each soldier’s costume (view) is under the management of the RecyclerView.ViewHolder. Ensuring that each soldier looks the part and that no costume goes to waste.

In short, The LayoutManager orchestrates the soldier’s positions, the RecyclerView.Adapter crafts the narrative, and the RecyclerView.ViewHolder brings each view to life. Together, these main components form the backbone of our production.

Lastly we have a DiffUtil. Imagine DiffUtil as a script harmonizer of our theater. When our data undergoes modifications — new characters joining the scene, existing ones taking on new roles, or bidding farewell — DiffUtil steps in. It takes two versions of our script as input and calculates the difference with precision. This calculated difference becomes the director’s cue to RecyclerView, triggering minimal animations and updates.DiffUtil can calculate the difference between versions of the list. DiffUtil takes both lists as input and computes the difference.

RecyclerView offers three main approaches for diffing the versions of the list. There are three primary ways to do this for RecyclerView. The recommended starting point is to use ListAdapter, which is a higher-level API. ListAdapter contains list diffing on a background thread, minimizing the amount of code you need to write for efficient handling of changes in the dataset.

In the Android app development, the RecyclerView takes center stage, transforming the task of displaying extensive datasets into a seamless performance. From orchestrating soldier positions to crafting the narrative and managing costumes intelligently, the LayoutManager, RecyclerView.Adapter, and RecyclerView.ViewHolder form an amazing trio, ensuring a captivating and efficient production. With DiffUtil as the script harmonizer of script changes, effortlessly comparing even the longest lists.

References

https://developer.android.com/develop/ui/views/layout/recyclerview

https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView

--

--