The item click event of Android recyclerview implements sorting

Since you started using recyclerview instead of listview, you will find that there are many places to learn. The learning records of the previous period include:

Research on rolling events of recyclerview - Packaging Optimization of viewholder and adapter of devwiki recyclerview - devwiki recyclerview problem record - devwiki

There are three ways to implement the click event of an item of recyclerview:

1. Add click listening when creating ItemView

The idea is: because viewholder can get the root layout of each item, if we set a separate onclick listener for the root layout and open it to the adapter, we can not set the itemclicklistener when assembling the recyclerview, but the listener is not set to the recyclerview, but to the adapter. The specific implementation code is as follows:

2. Implemented when ItemView attaches recyclerview

The implementation method was found when reading a foreign blog. The original link is as follows: the code of getting your clicks on recyclerview is as follows:

In the above code, onchildatachstatechangelistener event listening is set for recyclerview. Event listening is set when the child view attaches to recyclerview.

When you use it, you only need to call the addTo (RecycleView view) method to get the ItemClickSupport object, then call the setOnItemClickListener () method and setOnItemLongClickListener () method to set ItemView click events and long click events to listen.

3. It is implemented through the existing method addonitemtouchlistener() of recyclerview

3.1. View the source code

Viewing the recyclerview source code, you can see that recyclerview reserves a touch event method for an item:

Through annotations, we can see that this method is invoked before rolling events and needs to be passed into a OnItemTouchListener object. The code of onitemtouchlistener is as follows:

This interface also provides an implementation class, which is officially recommended as simpleonitemtouchlistener:

3.2 understand the working principle of gesturedetector

For the touch screen, its native messages are nothing more than press, lift and move. We only need to simply reload ontouch or set the touch listener setontouchlistener to process them. However, in order to improve the user experience of our app, sometimes we need to recognize users' gestures. The gesture recognition tool gesturedetector provided by Android can be of great help.

The working principle of gesturedetector is that when we receive a user touch message, we give the message to gesturedetector for processing. We set a listener to obtain the gesture processed by gesturedetector.

Gesturedetector provides two listener interfaces. Ongesturelistener handles click class messages and ondoubletaplistener handles double-click class messages. The interfaces of ongesturelistener are as follows:

Sometimes we don't need to handle all the above gestures. For convenience, Android provides another class simpleongesturelistener, which implements the above interface. We just need to inherit simpleongesturelistener and overload the required gestures.

3.3. Implement click event monitoring

After understanding the working principle of gesturedetector, you can start to implement the click event of recycleview's item. First, write a simplerecycleviewitemclicklistener class to inherit simpleonitemtouchlistener. When constructing, pass in an item, click to callback onitemclicklistener, and override the Boolean onintercepttouchevent (recyclerview RV, motionevent E) method of the parent class. The specific code is as follows:

In the gesture callback of gesturedetectorcompat, we override:

If we only need to listen to click events instead of long press events and double-click events, we only need to pass in simpleonitemclicklistener when constructing simplerecyclevitemclicklistener. If we need to handle other gesture monitoring, we can also override the corresponding gesture callback method.

4. Comparison of three methods

The above three methods are:

It can be seen from the implementation process of the above three methods:

The three methods can monitor the click event and long press event of ItemView.

The first and second methods are convenient for listening to subviews in ItemView.

The third method can easily obtain the coordinates clicked by the user.

The second and third methods can be written in separate classes. Compared with the first method written in the adapter, it can make the code more independent and tidy.

in summary:

If you only want to listen to the click event or long press event of ItemView, you can use all three methods.

If you want to listen to the click events of each sub view in ItemView, it is more convenient to use the first or second method.

Thank you for reading, hope to help you, thank you for your support to this site!

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