Android recyclerview implements drag sorting and sideslip deletion examples

When developing applications, we often encounter the needs of list sorting and sliding deletion. If the listview is used for the list effect, you need to customize the view to achieve the effect; However, if recyclerview is used, the system API has provided us with corresponding functions.

Next, let's take a look at how to use the system API to achieve the effect of sorting and deletion.

Create itemtouchhelper

Create an itemtouchhelper object and call the attachtorecyclerview method:

When creating the itemtouchhelper object, we need to pass in an object that implements the itemtouchhelper. Callback interface. The sorting and deletion logic is encapsulated in the itemtouchhelper. Callback object.

Implement the itemtouchhelper.callback interface

Create myitemtouchcallback class and implement itemtouchhelper.callback interface:

After implementing the itemtouchhelper.callback interface, three methods need to be rewritten:

override method

Let's first look at the getmovementflags (recyclerview, recyclerview. Viewholder, viewholder) method:

The code is divided into two cases according to layoutmanager:

The following is the onmove (recyclerview, recyclerview. Viewholder target) method:

As mentioned earlier, the onmove (recyclerview, recyclerview. Viewholder target) method is that the user will call back when dragging an item. So the code for list sorting should be written here. The viewholder in the method parameter represents the item currently dragged by the user, while the target represents the item overwritten by the user's drag. Therefore, the logic in the onmove (recyclerview, recyclerview. Viewholder target) method is to change their positions from fromposition to toposition.

Finally, the onshipped (recyclerview. Viewholder, viewholder, int direction) method:

This method will call back when the user performs sideslip deletion. The logic is to get the item that the current user performs sideslip deletion, and then delete it.

Here, it's done. Let's see the effect:

design sketch

Improve user experience

We found that there are still some imperfections: for example, when users are dragging and sorting, they can change the transparency of the current dragged item, so that they can be distinguished from other items. Then, we need to override the onselectedchanged (recyclerview. Viewholder, viewholder, int actionstate) method:

Correspondingly, when the user's finger is lifted from the dragged item, we need to restore the transparency of the item. We need to rewrite the Clearview (recyclerview, recyclerview, recyclerview. Viewholder, viewholder) method:

OK, let's see the effect after improvement:

Improvement effect drawing

Source code download: testrv.rar

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