Android recyclerview basic usage details

What is recyclerview

Recyclerview is a control mainly used to display and recycle views. It is summarized in one sentence

Recyclerview is a view that effectively displays large data sets by providing a limited data item window. The base provides several basic functions,

Control how it is displayed ―― > layoutmanager ― (required) control the interval between items ― > itemdecoration ― - (not required) control the animation of adding and deleting items ― > itemanimator ― (not required)

The above can complete most of the functions of listview, but a very common function is missing, that is, the on-demand event of item. Of course, this is not a problem. As mentioned above, recyclerview is used to control the recycling of view display, and others are customized by users. It gives users the freedom to be. Let's look at some basic ways to make.

Basic use

Because recyclerview is not in the basic library of Android, if developers need to use recyclerview, they need gradle to refer to the corresponding extension package,

compile 'com.android.support:recyclerview-v7:25.0.1'

After that, there is little difference between what you need to do and using listview. You only need to set layoutmanager and adapter to complete the most basic display. The adapter and the viewholder required by the adapter inherit from the internal class of recyvlerview.

Adapter for recyclerview

Viewholder of recyclerview

Project master activity

Activity layout file layout

Item layout file layout

The following is the operation effect

The basic display has been completed above. Here is how to add click events to recycler. In fact, the idea is very simple, that is, make an article on addonitemtouchlistener on recyclerview. The specific implementation is given here. I believe you can understand it at a glance.

*Implementation of onitemtouchlistener*

Mode of use

First, inherit recycleritemclicklistener.onitemclicklistener in the main class, and then add the following methods in the main class.

Basic usage reference: https://www.oudahe.com/p/16478/ Click Yes reference: https://www.oudahe.com/p/26551/

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