Detailed explanation of Android recyclerview linear layout (1)

Recyclerview is a new control in Android 5.0, which is under android-support-v7. The official document gives a concise introduction to recycleview, as follows:

A flexible view for providing a limited window into a large data set.

The following is the reccleview inheritance diagram:

Seeing this, we naturally think of a similar control listview. Recyclerview and listview maintain a small number of views in a limited screen area to display a large amount of data. In fact, recyclerview is an upgraded version of listview, which is more powerful, flexible and extensible. Google recommends using recycleview instead of listview.

The following is the structure diagram of recycleview:

Recycleview mainly includes the following components:

1. RecyclerView.Adapter

The separation design of data and view is an important indicator of program decoupling and maintainability. As a view displaying a large amount of data, recycleview must follow this design, and listview is no exception. You can easily implement your own adapter by inheriting recycleview.adapter. You can mainly override the following three methods of recycleview.adapter:

The myrecycleview adapter above is my own defined viewholder

2. ViewHolder

Viewholder is used to save the class referenced by item view in the list. The purpose of this is to reuse item view and improve performance. In the listview, the viewholder is not built in the listview and needs to be defined by itself. Of course, you can also not use the viewholder. The consequence is that every time the listview calls the getview() method, it will call the findviewbyid() method. You should know that the findviewbyid() has poor performance. It recursively finds a specific sub view from the view tree, In the end, it often brings poor performance experience, so using viewholder has become the most important means to optimize listview. In recyclerview, Android has built-in recyclerview.viewholder, which means that the use of viewholder has become a must.

3. Split line recycleview.itemdecoration

Adding a split line to the listview is very simple. You only need to configure the "divider" attribute in the XML of the listview, such as:

However, adding split lines to recycleview is troublesome, and it does not have split lines by default. However, split lines are generally required in actual development. Use recycleview.itemdecoration to add split lines for recycleview

4. Layout manager layoutmanager

Recycleview supports multiple layout modes:

1. Linearlayoutmanager linear layout manager supports horizontal layout and vertical layout. 2. Gridlayoutmanager grid layout manager 3. Stacgerdgridlayoutmanager staggered grid layout manager, which can realize waterfall flow list

Recycleview is similar to listview when used. Take a direct look at a simple example to show a list in the vertical and horizontal directions respectively, and both support the use of colors and pictures as split lines. The effect diagram is as follows:

1. MainActivity

Mainactivity layout file r.layout.activity_ main

2. Vertical fragment

Layout file r.layout.fragment of verticalfragment_ vertical_ layout

3. Directly inherit recyclerview.adapter

Item layout file item_ vertical_ recycleview.xml

4. Dividing line recyclerview.itemdecoration

Unfinished to be continued

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