Recyclerview User Guide

Recently, I read a lot of articles on the use of recyclerview. I've been dizzy and don't know what the routine is. Many people write the code directly, but they don't explain the use of the code in detail, so they intend to write it themselves. By the way, help those newcomers who are learning Android.

For the source code of this article, see https://github.com/huanshen/Learn-Android/tree/master/recycleTest

Firstly, recycleview needs to be introduced, so it is introduced in build.gradle (model):

Let's start writing the layout file:

This is equivalent to occupying a place on the screen for recyclerview, but we have to add an item item item for it. The item also has its own layout, so we continue to write the following layout.

After the layout is written, the next step is how to display the data on the view. Of course, you need to use the adapter. Recycleview has its own adapter. We just need to inherit it, and then write specific processing code. The details are as follows, which will be analyzed in detail later.

Here, let's analyze in detail what to write after inheritance. The first is the constructor myadapter, which is used to pass in the required data. Generally, it is an array or linked list, so as to form a list.

The second is oncreateviewholder, which I translated into "create view container", which is used to hold the item view. First get the view of item, and then put it into the container.

The next step is onbindviewholder, which is to bind each item of the item in the container so that we can map the data to the view for display.

Then there is getitemcount, which actually returns a quantity, that is, how many items are created in the end.

Finally, we customized a viewholder, which is inherited from recyclerview.viewholder. This is better understood, that is, we need to put each item in the item into the viewholder container first, so that we can bind the above. An mtextview is defined here. Note that it also appears in onbindviewholder.

Take a good look at the code, and then read the above analysis again. You should know how to use it.

Next, we'll start using them. See the code below:

First, we have to find the original recyclerview container, and then we create a default linear layoutmanger and set recyclerview to linear. Then we import data into MyAdapter and create an instance of it, and finally call setAdapter.

What we showed earlier is that there is only one item and the styles are the same. Can we have many different styles? The answer is yes.

As shown in the figure above, we let it adopt different styles for parity. Of course, we are still free to realize graph and graph free.

How to implement this, or modify it based on the previous code.

First, we add a new style called Item1. XML

Then we began to add the code to implement multiple items of the face, mainly in the adapter we wrote ourselves. The activity code doesn't need to be moved.

First, we define two constants to represent two different types. Because there are two types, it is natural to define two different viewwhold. The specific codes are as follows:

Similarly, we need to create two different viewhold containers. How can we know which one we need to create?

At this time, getitemviewtype can be used. This function determines different types for us according to different locations.

Because our data is relatively simple, I divide it by parity. In case of real operation, we must provide data and data category at the same time, so that we can know which view is used to display the data.

When we have defined the data type, we can create viewhold.

In the above code, we refer to different layouts according to different types.

Once created, the data is bound. The specific codes are as follows:

When binding, we need to determine the type of viewhold, so that we can correctly bind the data to the view.

Well, here we are. I believe you should be able to master recycleview.

When each item is clicked, the corresponding position will pop up.

First, we need to implement the view. Onclicklistener interface so that items can be clicked.

Here, we implement the interface in imageholder and override the onclick method. The contents of the method will be discussed later. The parameter of this method has no position, so we need to do some processing. Position position is obtained mainly through getadapterposition.

We define an item click interface

Here we define an onrecyclerviewitemclick interface, and its internal method is onItemClick. You can see that we have a position parameter.

Then, during initialization, we pass the instance onrvitemclick of onrecyclerviewitemclick to the private parameter monrvitemclick; Then call onclick. See the above for details.

Finally, we can call.

Recyclerview has a default split line method: divideritemdecoration.

Simply call this:

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
分享
二维码
< <上一篇
下一篇>>