Comparison between Android listview and recycleview

Listview, just like its name, is a view used to display lists, and recycleview is an enhanced version of it. Today, it brings the comparative use of these two views with almost the same functions

Let's start with listview

ListView:

1. Use listview in the layout file and define an ID for it to facilitate our subsequent calls. The width and height are the same as the parent control

2. Prepare the data and add the data to the arrayadapter adapter

3. Use findviewbyid in the java file of activity to find the listview instance and set the adapter for it

4. Implement the item click event of listview, and directly use the setitemclick method defined by listview

The most important step here is actually the second step. We can see that the following code defines a set of arrays of string type, and then creates an arrayadapter adapter. Note that string is a generic type, and the parameters in the subsequent constructor are context android.r.layout.simple_ list_ Item 1 is an item layout given by the Android system. The last item is a string array, that is, data. Then, find an instance of listview and call the setadapter method to set the adapter

This is a simple example. However, we know that the second parameter is an item layout provided by Android. This layout is too simple. What should we do if we want to implement a custom layout? We need to modify the arrayadapter adapter

Here are the steps to customize the layout and use it:

1. Create a layout of item, that is, our custom layout

I defined two textviews with half of each. Remember the high use wrap of the root tag_ content

2. Define a t class as the generic type of the adapter (similar to the previous string), which requires a constructor, member variable and get method

I don't know why there are warnings. The public can be used normally. If it is changed to another one, there will be an error calling there

3. Create an adapter class to inherit arrayadapter < T > (t is the class defined in step 2), which requires a construction method and a copy of the getview method

4. Prepare the data and add the data to the adapter class in step 3. Then, as in the above steps, find the instance of listview and set the adapter

effect

RecycleView:

Since this is a new product launched by the Android team, we need to add dependencies before we can use it. Remember to click sync now

Use steps:

1. Use recyclerview in the layout file to define the ID with the same width and height as the space

2. Prepare the layout of the sub item item item of recyclerview, and then use it in the adapter class, which will not be described here

3. Define a generic class, such as the t class in listview, which is the generic type of the adapter

3. Define an adapter t class to inherit recyclerview. Adapter < t.viewholder > (viewholder is an internal class in t class). A constructor is required to receive the data source and copy the three methods

Recycleradapter inherits recyclerview. Adapter < recycleradapter. Viewholder >

The one in the angle brackets should have been recyclerview.viewholder, but we defined an internal class viewholder in the recycleradapter to inherit recyclerview.viewholder, so we can call the internal class directly in the angle brackets

Let's first take a look at the internal class viewholder, which has two textview member variables. Then, add a constructor with a view as the parameter to find instances for the two textviews through findviewbyid

Let's look at the recycleadapter class. At the beginning, we declared a list member variable students. Then, we added a constructor, passed in a list, and assigned it to the recycleadapter member variable students

After that, you need to duplicate three methods, oncreateviewholder, onbindviewholder and getitemcount

In the oncreateviewholder method, first, a context is received through the layoutinflator. From () method, then the item layout is put into the general layout through the inflate method, and then the view is passed to the viewholder as a parameter. At the same time, a viewholder is returned

The onbindviewholder method uses the get method of the list to obtain an object in the list. Then, the holder calls the settext methods of two textviews to set it. Draw inferences from one example. If it is an ImageView, you can also use setresource to set the displayed picture and set the parameters, which can be directly obtained by the get method in the student class

Getitemcount is to obtain the quantity of all items. Here, the item quantity is actually the item in the list. Just return its size directly

4. Prepare data and add it to the adapter

5. Create a layout manager layoutmanager to facilitate recycleview to set the layout. There are several layout managers here

A vertical arrangement like listview, linear layout manager

We can also change it to horizontal arrangement. Just call setorientation of linearlayoutmanger and set it to linearlayoutmanager.horizontal

There are also giidlayout grid layout, staggergildlayoutmanager waterfall flow layout. The use method is as above. However, the parameters are different. For more information, please turn to Baidu

6. Find the instance of recycleview through findviewbyid method, set layoutmanager and adapter for it

7. Set the onclick method. There is no onclick method in recycleview. We need to write it ourselves. Where can we write it? In the onbindviewholder method in the recycleadapter

summary

The above is the comparison and analysis of Android listview and recycleview introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time!

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