Android recyclerview click event
1、 Overview
With the release of Android l version, recyclerview has gradually replaced listview to display more data sets. Compared with listview, recyclerview has greatly improved its performance. It can be said that recyclerview is an upgraded version of abslistview. Recyclerview has its own viewholder. Different from listview cache convertview, recyclerview caches the viewholder and the operation object is also the viewholder. Although listview also has the function of caching convertview, when using listview, display, caching, recycling and layout are coupled together; Recyclerview can understand it and operate more flexibly, so that developers can better customize various effects. In addition, recyclerview provides local refresh. The relationship is shown in the figure below:
2、 Basic use
Recyclerview provides the following roles: 1. Recyclerview.adapter adapter 2. Recyclerview.layoutmanager, which is used to manage layout display. The following methods are officially provided
Linearlayoutmanager displays list items that scroll vertically or horizontally. Gridlayoutmanager displays them as a grid. Staggeredgridlayoutmanager displays them as a staggered grid
At the same time, developers can also customize layoutmanager and inherit recyclerview.layoutmanager.
3. Recycler.itemdecoration is an additional sub view of each item, which can be used to draw dividers and set padding, etc. 4. Recyclerview.itemanimator is responsible for the animation effect when adding and deleting data
See the official documents for specific usage
Used in project
In activity
Custom adapter
Item animation can be added manually if necessary. This is not the focus today. Here are two good open source projects attached here and here
Click event of recyclerview
There is no onitemclicklistener callback method similar to listview in the official document. Because recyclerview is more advanced than listview, it has no concept of row or column. Sub views can be laid out arbitrarily. Each sub view handles its own onclick event, that is, set a click callback for the rootview of the sub view in the adapter. What we want to implement today is another way, similar to the onitemclicklistener of listview. Through the documentation, we know that recyclerview leaves the developer with a recyclerview.onitemtouchlistener interface. All we have to do is implement it to realize the click callback and long press callback. Of course, this method is just the beginning. We can also expand it to callback for various complex gesture operations
We registered the gesture operation in the onintercepttouchevent method. When there is a specific gesture, we can receive it through the simplegesturelistener callback interface, in which we implemented click and long press, and then callback our own defined interface. It's also easy to use
For gesture operation, we can define more operation callbacks for ItemView.
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.