Android Recyclerview Sliding Delete and drag sort _android

Source: Internet
Author: User
Tags call back

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

First look at the effect of this article:

The effect content mainly has three parts:
• Click on one of the item to drag it to another location
• Slide to left and right to delete an item
• The long press will have a floating action, put down will be back to align

① first easy after difficult, card float effect
in material design, objects are rendered in 3d mode, which means that a Z axis is added to the original 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 effect diagram of the touch feedback is the first wave pattern, then when the card can move the first float, then start moving, the final whereabouts, the effect is as follows: (floating animation may have to look carefully)

Realize:
The water ripple effect is provided by the system because a cardview is used in the demo 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 not difficult, changing the view's Translationz property:

private void Pickupanimation (view view) {
  Objectanimator animator = objectanimator.offloat (view, Translationz, 1f , 10f);
  Animator.setinterpolator (New Decelerateinterpolator ());
  Animator.setduration ();
  Animator.start ();
}

② sliding (swipe) and moving (move) Item
swipe means that we slide the top to the left and move the item by moving it, changing the order of the list. To implement these two features, you need to use a class itemtouchhelper, and when we construct this class, we can call its Attachtorecyclerview method to bind it to a recyclerview. When the recyclerview occurs (sliding and moving), the callback class that is used to construct the class will call back the corresponding method, in which we operate the dataset.
New Itemtouchhelper (new Itemtouchhelper.callback () {//ellipsis code
}). Attachtorecyclerview (Mrecyclerview);
Then there is the way to rewrite the interface callback, and here are a few that need to be rewritten, respectively:
Whether the Isitemviewswipeenable:item can slide
Whether the Islongpressdragenable:item can be long pressed
getmovementflags: Get the Move flag
OnMove: Callback When an item is replaced by another item, which is the order in which the contents of the dataset change
onmoved: Callback when OnMove returns True
onswiped: Callback When an item is slid off the screen
Setselectedchange: An item is invoked when it is long pressed, and is called when an item that is long pressed to move is released

New Itemtouchhelper (New Itemtouchhelper.callback () {private Recyclerview.viewholder vh;
  @Override public boolean isitemviewswipeenabled () {true;
  @Override public boolean islongpressdragenabled () {true;  @Override public int Getmovementflags (Recyclerview recyclerview, Recyclerview.viewholder viewholder) {// Drag-and-drop markers, here allow four directions up and down the int dragflags = Itemtouchhelper.up | Itemtouchhelper.down |
        Itemtouchhelper.left |
    Itemtouchhelper.right; Slide the marker here allows the left and right sliding int swipeflags = Itemtouchhelper.start |
    Itemtouchhelper.end;
  Return Makemovementflags (Dragflags, swipeflags); /* This method will be recalled when an item is dragged and moved, where we use to play the animation, when the Viewholder is not empty, or to release the state/@Override public void onselected
    Changed (recyclerview.viewholder viewholder, int actionstate) {super.onselectedchanged (Viewholder, actionState);
      if (Viewholder!= null) {VH = Viewholder;
    Pickupanimation (Viewholder.itemview);
  } else {    if (VH!= null) {putdownanimation (Vh.itemview);
             @Override public boolean onmove (Recyclerview recyclerview, Recyclerview.viewholder Viewholder, Recyclerview.viewholder target) {//change the corresponding position in the list when moving and return True Collections.swap (Newslist, Viewholder.getadapterposi
    tion (), Target. Getadapterposition ());
  return true; /* * Call @Override public void onmoved (Recyclerview recyclerview, Recyclerview.viewholder VI, when OnMove returns True) Ewholder, int frompos, recyclerview.viewholder target, int toPos, int x, int y) {super.onmoved (Recyclerview, VI
    Ewholder, Frompos, Target, ToPos, x, y);
  Refreshes the list after the move completes madapter.notifyitemmoved (Viewholder.getadapterposition (), Target. Getadapterposition ()); @Override public void onswiped (Recyclerview.viewholder viewholder, int direction) {//Remove data from the dataset Newslist.
    Remove (viewholder.getadapterposition ()); Refresh List madapter.notifyitemremoved (viewholder.gEtadapterposition ());

 }). Attachtorecyclerview (Mrecyclerview);

Here the code directly, and made comments, should be relatively simple.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.