Android single recyclerview realizes the effect of list nesting

Many times, you will encounter a demand that there is a list in the list. Before this demand, it is usually realized by nesting multiple list controls, but it is easy to have some problems, such as rolling conflict, incomplete data display, redundant logic processing, etc. Later, it was found that a recyclerview can achieve the effect of list nesting. Here, the multi layout function of recyclerview is required.

design sketch:

The main method involved in the multi layout of recyclerview is getitemviewtype, which is used to set the layout type to be displayed for each item. When I didn't know it before, I used mathematical logic to calculate directly. After how many positions, what layout is displayed. This method is suitable for when the logic is simple, but once the logic is a little complex, it can't be used decisively. I may bury a deep pit myself and it's hard to maintain, so I put the layout type in the data object here.

1. Define the base class of multiple layout objects:

Type is the layout type corresponding to the object.

2. Display the recyclerview data in viewholder and define the holder base class

Multiple objects may be involved in multiple layouts, so the object type in the base class is defined by generic type and must be a subclass of the base class of multiple layout objects, which is more convenient for later data and control binding.

3. Start creating a multi layout adapter

First of all, this involves layout types: header, content list and bottom. Define three types

Create the corresponding viewholder object according to the layout type

Of course, create the corresponding holder class in advance:

Here, erase the generic object and use the specific object onemodel as the current data object. Onemodel is the base class of basemuldatamodel.

Definition of onemodel:

4. Data processing

The data returned from the background is generally not in the format we want, so we split the data ourselves. The data processing method largely determines the difficulty of coding.

The data processing here is to put the data that needs to be displayed into the data collection list in order, and then set the type for each object to define the layout type it needs. The data processing method is relatively simple, but it can cope with many scenarios. In the shopping cart scenario, it is generally the same as the example, with a head, content and bottom. The data returned in the background may be a JSON object containing all (header, content list and bottom). Here, it is divided into three parts and displayed in the collection in turn.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>