Android recyclerview slide delete and drag sort

This is an introduction to recyclerview after the above three articles. Here are two more words. If you are still using listview, you can give up listview. Recyclerview automatically helps us cache the item view (viewholder), allows us to customize the animation and split line of various actions, and allows us to perform some gesture operations on the item. In addition, the launch of the design library greatly facilitates us to write apps with material style, while listview is incompatible with this library. For example, only recyclerview can coordinate sliding.

Let's first look at the renderings of this article:

The effect content mainly includes three parts: • long press and click an item to drag it to other places • slide left and right to delete an item • long press will have a floating action, and it will be realigned after dropping

① Easy first and then difficult. Card floating effect in material design, the object is presented in 3D mode, that is, a Z-axis is added to represent the height of the object on the basis of the original.

When we click on a card, we should give users some feedback to let users know that they are operating the card, that is, touch feedback. The touch feedback of the touch effect picture is that the water ripple first appears, then when the card can be moved, it floats first, then starts to move, and finally falls. The effects are as follows: (the floating animation may need to be seen carefully)

Implementation: the water ripple effect is provided by the system. Because a cardview is used in the demo to present the content, only two attributes need to be added to the cardview: Android: clickable = "true" Android: foreground = "? Android: attr / selectableitembackground" it is not difficult to float and sink the animation. Change the translationz attribute of the view:

② Swipe and move items swipe refers to the left and right sliding deletion above us, and move refers to the movement of items to change the sorting of the list. To realize these two functions, we need to use a class itemtouchhelper. After we construct this class, we can call its attachtorecyclerview method to bind it to a recyclerview. When some operations (sliding and moving) occur in the recyclerview, the incoming callback class will call back the corresponding methods when constructing this class, We can operate on the data set in these methods. New itemtouchhelper (New itemtouchhelper. Callback() {/ / omit code}). Attachtorecyclerview (mrcyclerview); The next step is to rewrite the callback method of the interface. There are several methods that need to be rewritten, namely: • isitemviewswipeenability: whether the item can slide • islongpressdragenable: whether the item can be long pressed • getmovementflags: get the move flag • onmove: callback when an item is replaced by another item, That is, the content order of the dataset changes • onmoved: callback when onmove returns true • onshipped: callback when an item is slid away from the screen • setselectedchange: callback when an item is selected by long pressing, and also called when an item moved by long pressing is released

The code is directly posted here and commented. It should be relatively simple.

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