Android development implements “drag and drop” and “slide delete” on recyclerview – 2

In the previous article, I introduced Android and took you step by step to implement the "drag and drop" and "slide delete" functions on recyclerview

The effects are as follows:

Drag handle

When designing a list that supports "drag and drop", it usually provides a "drag handle" that initializes drag when touching. It is recommended by material guidelines for its discoverability and availability, especially when the list is in "editable mode"

First update the layout of item (item_main. XML):

Pictures used as "drag and drop handles" can be found in the material design icon or easily added through the Android material design icon generator plugin

As we mentioned before, you can start dragging through the code itemtouchhelper. Startdrag (recyclerview. Viewholder). So what we need to do is update the viewholder to include a new handle view, and set a simple touch event interface to trigger the startdrag () method

We need to define an interface to pass drag events

Then, materialize the handle view in itemviewholder

And update the adapter

The complete adapter should look like this

The rest is to add onstartdraglistener to fragment

But after you run it, you can see this effect:

Mark selected view

In the basic example in the previous article, the dragged item is actually selected, but there is no visual indication. For obvious reasons, this is not popular, but it is also easy to repair. In fact, with the help of itemtouch helper, as long as your viewholder's ItemView has a background set (selector), you will get the corresponding effect. In lollipop and later versions, The elevation of item view will increase during dragging and sliding. In previous versions, there will be a fade effect when sliding. It looks like:

The effect looks good, but you may want more control. One way is to let the item handle the change whenever the viewholder is selected or cleared. Therefore, itemtouchhelper.callback provides two callbacks

Onselectedchanged (recyclerview. Viewholder, int). This method will be called every time the viewholder's state becomes drag (action_state_drag) or swipe (action_state_swipe). This is the perfect time to turn the viewholder's state into active

Clearview (recyclerview, recyclerview. Viewholder). When the dragged viewholder is put down, or when the sliding operation is cancelled or completed (action_state_idle), this is the best place to set the ItemView status to idle

Then, we will bind the two together

First, create an interface for the target viewholder to implement:

Next, trigger the corresponding callback method:

Now, what's left is to let itemviewholder implement itemtouchhelperviewholder:

Considering that this is just an example, we only set the gray background when the item is in the active state, and delete the gray background when the item is cleared. If your adapter and itemtouchhelper are closely paired, you can easily abandon this setting and change the state of the item directly in itemtouchhelper.callback

Grid layout

If you want to use gridlayoutmanager for transit in this project, you will find that it does not work normally. The reason and repair reason are very simple: you must tell itemtouchhelper that we want to support left-right dragging. In simpletouchhelpercallback, we have stated:

The only change required to support grid layout is to add left and right directions in dragflags:

However, for grid, "sliding deletion" is not a natural function mode, so you may write some code as follows:

The "drag and drop" effect on the grid looks as follows:

Custom slide animation

For us, itemtouchhelper.callback provides us with a very convenient way to fully control the animation of the viewholder when dragging and sliding the viewholder. Because itemtouchhelper is an itemdecoration, we can use a similar way to understand the hook into the view drawing in detail

The following is a simple example to override the default slide animation to show a linear fade effect

The parameters DX and Dy represent the current position of the selected view,

For any actionstate that is not processed, you can call the super method to use the default animation

summary

We've only learned about the interesting parts of what a custom itemtouch helper can do. I hope to include more content in an article, but considering the length of the article, it's better to separate it

The above is the Android development that Xiaobian introduced to you. It implements the "drag and drop" and "slide delete" functions on recyclerview (2). 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
分享
二维码
< <上一篇
下一篇>>