Android Development Learning Path-recyclerview sliding Delete and drag sorting

Source: Internet
Author: User
Tags call back

On the use of-recyclerview in the learning Path of Android development

Android Development Learning Path-recyclerview's item custom animation and Defaultitemanimator source analysis

Android Development Learning Path-dropdown refresh How do I do it?

This article is followed by the above three after the introduction of Recyclerview, here to say two more, if you are still using the ListView, you can discard the ListView. Recyclerview automatically helps us cache the item view (Viewholder), allowing us to customize animations and split lines for various actions, allowing us to do some gestures on item. In addition, because the introduction of the design library is very convenient for us to write the app with material style, and the ListView is incompatible with this library, such as sliding coordination, only Recyclerview can do.

Read this article first:

The effect content mainly has three parts:

    • Long press to click on one item to drag it to another place
    • Swipe left and right to delete an item
    • When you press and hold, there will be a floating action, which will be re-aligned when you drop it.

① first easy after difficult, card floating effect

In material design, the rendering of objects is performed in 3d mode, that is, adding a z-axis to the original base to represent the height of the object.

When we click on a card, we should give the user some feedback, let the user know that they are operating the card, that is, touch feedback. Touch feedback is the first occurrence of ripples, and then when the card can move the first float, then start moving, the final whereabouts, the effect is as follows: (floating animation may need to look carefully)

Realize:

The water ripple effect is provided by the system, because the demo uses a CardView to render the content, so you only need to add two attributes to the CardView:

Android:clickable= "true" android:foreground= "? Android:attr/selectableitembackground"

Floating and sinking animations are also easy, changing the Translationz properties of the view:

Private void pickupanimation (view view) {    = objectanimator.offloat (view, "Translationz", 1f, 10f);    Animator.setinterpolator (new  decelerateinterpolator ());    Animator.setduration (+);    Animator.start ();}

② Slide (Swipe) and move Item

Swipe refers to the left and right swipe delete above us, move is our move to item, change the sort of list. To implement these two functions, you need to use a class itemtouchhelper, and when we have constructed this class, we can call its Attachtorecyclerview method to bind it to a recyclerview. When there are certain operations (sliding and moving) in the Recyclerview, the callback class that is passed in when the class is constructed will callback the corresponding method, and we can manipulate the dataset in these methods.

New Itemtouchhelper (new itemtouchhelper.callback () {
// omit Code }). Attachtorecyclerview (Mrecyclerview);

Next is the way to rewrite the interface callback, there are several rewrite here, respectively:

    • Whether the Isitemviewswipeenable:item can slide
    • Whether the Islongpressdragenable:item can be pressed long
    • Getmovementflags: Get Move Flag
    • OnMove: Callback When an item is replaced by another item, that is, the content order of the data set changes
    • Onmoved: Callback When OnMove returns True
    • Onswiped: Callback When an item is slid off the screen
    • Setselectedchange: An item is long pressed and selected to be recalled, and is also called when an item that is moved by a long press is released
NewItemtouchhelper (NewItemtouchhelper.callback () {PrivateRecyclerview.viewholder VH; @Override Public Booleanisitemviewswipeenabled () {return true; } @Override Public Booleanislongpressdragenabled () {return true; } @Override Public intgetmovementflags (Recyclerview recyclerview, Recyclerview.viewholder viewholder) {//drag-and-drop markers, which allow up and down four directions        intDragflags = Itemtouchhelper.up | Itemtouchhelper.down | Itemtouchhelper.left |Itemtouchhelper.right; //Slide The mark, here allow left and right sliding        intSwipeflags = Itemtouchhelper.start |Itemtouchhelper.end; returnmakemovementflags (Dragflags, swipeflags); }    /*This method will call back when an item is dragged and moved, where we use it to play the animation, when the Viewholder is not empty, or the release state*/@Override Public voidOnselectedchanged (Recyclerview.viewholder Viewholder,intactionstate) {        Super. onselectedchanged (Viewholder, actionstate); if(Viewholder! =NULL) {VH=Viewholder;        Pickupanimation (Viewholder.itemview); } Else {            if(VH! =NULL) {putdownanimation (Vh.itemview); } }} @Override Public BooleanOnMove (Recyclerview recyclerview, Recyclerview.viewholder Viewholder, Recyclerview.viewholde R target) {//change the corresponding position in the list when moving and return TrueCollections.swap (Newslist, viewholder.getadapterposition (), Target. Getadapterposition ()); return true; }    /*called when OnMove returns True*/@Override Public voidOnmoved (Recyclerview Recyclerview, Recyclerview.viewholder Viewholder,intFrompos, Recyclerview.viewholder target,intToPos,intXinty) {Super. onmoved (Recyclerview, Viewholder, Frompos, Target, ToPos, x, y); //Refresh the list after the move is completemadapter.notifyitemmoved (Viewholder.getadapterposition (), Target. Getadapterposition ()); } @Override Public voidonswiped (Recyclerview.viewholder Viewholder,intdirection) {        //to remove data from a data setNewslist.remove (Viewholder.getadapterposition ()); //Refresh Listmadapter.notifyitemremoved (Viewholder.getadapterposition ()); }}). Attachtorecyclerview (Mrecyclerview);

The code is directly posted here and commented, it should be easier.

If the content of the article is wrong, please point out, thank you.

Android Development Learning Path-recyclerview sliding Delete and drag sorting

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.