Android takes you step by step to implement the “drag and drop” and “slide delete” functions on recyclerview

First, let's show you the general effect drawing:

There are many tutorials, libraries and examples on Android, and the "drag and drop" and "slide delete" functions are implemented on recyclerview. Although there are updates and better methods available, most people still use the old view. Ondraglistener and the swipetodismiss method of Roman nurik. In addition to the frequent use of gesturedetector and oninterceptotouchevent, few people use the new API, Otherwise, the implementation will be complicated. In fact, there is a very simple way to add these two functions to recyclerview. It only requires one class, and this class is already part of the Android support package

ItemTouchHelper

Itemtouchhelper is a powerful general-purpose program. It will handle all the things you need to do when adding "drag and drop" and "slide delete" on recyclerview. It is a subclass of recyclerview.itemdecoration, which means that it can be easily added to any existing layoutmanager and adapter! It does not affect the animation added to the item, and supports strict category "drag" and "drop" animation, and can support more

get ready:

First, what we need is to add the dependencies of recyclerview:

Using itemtouchhelper and itemtouchhelper. Callback:

In order to use itemtouchhelper, you will create an itemtouchhelper. Callback, which is an interface that allows you to listen to "move" and "swipe" events, and you can control the state of the selected view through callback, and you can change the default animation of the view. If you just want a basic implementation, you can use the help class simplecallback, But in order to learn how callback works, we will implement one ourselves

In order to activate the basic "drag and drop" and "slide delete", the main methods we must override are:

We will also use these two methods:

Let's take a look one by one:

Itemtouchhelper allows you to easily determine the direction of events. You must implement the getmovementflags (recyclerview, recyclerview. Viewholder) method to indicate the supported directions of "drag" and "slide", and use itemtouchhelper.makemovementflags (int, int) to build the return tag. Here, we activate "drag" and "slide" in two different directions

Itemtouchhelper can be used to implement "drag without sliding" or "slide without dragging", so you must specify exactly the actions you want to support. If you want to support the "long press start drag and drop" event on the item of recyclerview, you must implement islongpressdragenabled() to return true. In addition, itemtouchhelper.startdrag (recyclerview. Viewholder) can be started from "operation" "Drag and drop", which will be described later

If you want any touch event inside the view to start the "sliding" action, simply return true in isitemviewswipeenabled(). In addition, itemtouchhelper.startswipe (recyclerview. Viewholder) can manually start the "sliding" event

Then, the onmove () and onshipped () methods need to be implemented to notify the things responsible for updating the basic data. Therefore, first, we need to create an interface to allow us to pass callbacks for "drag and drop" and "slide delete" events

From the current example, the simplest way to implement these is to implement the interface of our recyclerview.adapter:

It is very important to call notifyitemremoved (int) and notifyitemmoved (int, int), so the adapter will update the data. Please note that this is also important. We change the position of the item every time the view is switched to a new index, not after the "put" event

Now let's go back to building simpleitemtouchhelpercallback, because we still have to override onmove() and onshipped() methods. First, add a builder and variables for the adapter:

Then overwrite the remaining events and notify the adapter:

The callback should look like this:

When the callback is ready, we create an itemtouchhelper and call the attachtorecyclerview (recyclerview) method:

When you run, the result should look like this:

summary

This is a very simple implementation of itemtouch helper. However, we should be clear that it is completely unnecessary to use third parties and libraries to implement basic "drag and drop" and "slide delete" on recyclerview

Please click here for sample code

The above is what Xiaobian introduced to you. Android takes you step by step to realize the "drag and drop" and "slide delete" functions on recyclerview. 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
分享
二维码
< <上一篇
下一篇>>