Implementation of detailed explanation of Android recyclerview listview GridView waterfall flow effect

What is recyclerview

Recyclerview is the latest component introduced by Google to replace listview and GridView. Recyclerview is a container used to display a large amount of data and improve the performance of scrolling through a limited number of sub views.

Unlike listview, recyclerview is no longer responsible for layout, but focuses on layout reuse. Layouts are mainly managed through layoutmanager. At present, three common layout management methods are provided:

Linearlayoutmanager linear layout manager (listview effect) gridlayoutmanager grid layout manager (GridView effect) staggeredgridlayoutmanager waterfall flow manager recyclerview mainly obtains the sub views to be displayed through the adapter. The adapter needs to inherit the recyclerview.adapter class, and needs to develop a generic type of the subclass recyclerview.viewholder.

To use recyclerview, you need to import packages and add dependencies on build.gradle

The code is as follows:

Note: when the data changes, you can refresh the UI through notifydatasetchanged and get the type of the corresponding location through getitemviewtype. However, you no longer need to specify how many types there are, because this method can judge how many types there are.

The sub view layout is as follows: RES / layout / recycler

After the adapter is ready, prepare recyclerview. Add recyclerview to the layout of the activity

Then add the initialization and setting code in the activity

The effects after operation are as follows:

If you want to display as a GridView effect, just replace the linear layoutmanager with the gridlayoutmanager

The effects are as follows:

If you want to display waterfall flow, you need to change to staggeredgridlayoutmanager.

When using waterfall flow, the height should be different, so you can set the height of the sub view randomly. Modify methods in the adapter

The operation effect is as follows:

The above is the detailed explanation of Android recyclerview introduced by Xiaobian. It realizes the waterfall flow effect of listview GridView. 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. Thank you very much for your support for the programming tips website!

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