This is the fifth day of my participation in the August More text Challenge. For details, see:August is more challenging

The last article briefly introduced some basic views, which are all built in XML, and look very simple. Today, let’s start to step up the difficulty and enter the implementation of “lists”.

Features of lists

Lists are a very common UI style in apps, such as wechat conversation list, contact list, or Bilibili home page recommendation list, etc. Lists have two features: an infinite amount of data and repeated views,

On the surface, a list is just a copy of the same style View, as if it could be done using the CV method. But Android actually provides a ViewGroup for lists — RecyclerView — because the biggest challenge to implementing a list on Android is performance.

This is the first time the concept of performance has been mentioned. In Android development, performance is measured in many dimensions, but there is one clear manifestation: a lag in action. (All Android users have experienced this.)

Why is there a performance problem

Since the View itself is a Java object, it takes time to create the View, and it takes space to store the View. According to the logic of copying the View, if the list of data is infinite, the memory required is infinite.

How to solve performance problems

The list problem, the solution is also in the list feature. Because of the repetitive View feature, we can take the invisible View object out, populate it with new data, and display it in a new location.

Implementing a list

Effect display:

The implementation process

The same with other views is to add a RecyclerView in XML, but XML can not describe the content of RecyclerView.

Then add the contents of the list. Because we’re going to create reusable views, we’re not going to write a particular View, we’re going to write a method of creating a View. The creation method of this View is implemented by inheriting from the ViewHolder.

Views loaded in a ViewHolder can still be created using XML. Each ViewHolder can only create one View. Complex lists may require multiple styles. Instead of doing complex logical transformations in a single ViewHolder, use multiple ViewHolder implementations.

ViewHolder and RecyclerView through recyclerView. Adapter

connected together, Adapter is an abstract class, responsible for docking data and View, according to the requirement to rewrite the abstract function can achieve the most basic effect. Generic arguments are subtypes of a ViewHolder, so when using multiple ViewHolder, you may need to define a base class and then strong-type the data.

Finally set Adapter to RecyclerView, complete code on Github.


RecyclerView is a kind of “unfathomable” View, the basic use is just the surface, more usage we talk about in detail in practice.

The coming weekend, Saturday and Sunday plan to write practice, the number of words will be more, remember to see ah brothers, ball ball everyone.