Android Imitation QQ list left-slide delete operation _android

Source: Internet
Author: User
Tags abs touch

Recently learned how to do a like QQ left-sliding recyclerview item display options, mainly used to scroller

We start with a new recyclerview.

Define a few variables to use
Rewrite the constructor method to change the first two constructs to read as follows, so that the construction is done in any case the third construct method
Initialize scroller in the third construct method

public class Leftswipemenurecyclerview extends Recyclerview {//Sticky button private TextView tvtop;
  Delete button private TextView tvdelete;
  Item corresponding layout private LinearLayout mitemlayout;

  Maximum width of the menu private int mmaxlength;
  The x coordinate of the last touch behavior is private int mlastx;

  The y-coordinate of the last touch behavior is private int mlasty;

  The position of the currently touched item is private int mposition;
  Whether in the vertical slide list private Boolean isdragging;
  Item is a private Boolean isitemmoving that follows the finger movement;

  Item whether to start automatically sliding private Boolean isstartscroll;
  Left-sliding menu State 0: Close 1: Will close 2: will open 3: open private int mmenustate;
  private static int menu_closed = 0;
  private static int menu_will_closed = 1;
  private static int menu_open = 2;

  private static int menu_will_open = 3;

  To achieve elastic sliding, restore private scroller mscroller;

  Item of the event monitor private Onitemactionlistener Mlistener;
  Public Leftswipemenurecyclerview {This (context, NULL); "Public Leftswipemenurecyclerview" (context, @Nullable AttributeSet attrs) {The context,Attrs, 0); Public Leftswipemenurecyclerview (context, @Nullable attributeset attrs, int defstyle) {Super (context, at
    TRS, Defstyle);
  Mscroller = new Scroller (context, new Linearinterpolator ());

 }

Overriding the Ontouchevent method

Event mainly has the following several action

    • Action_down Finger contact to screen
    • Action_move Finger on screen slide
    • Action_up Finger off the screen

To get the relative coordinates of X and Y at first,

int x= (int) event.getx ();
int y= (int) event.gety ();

And then deal with 3 different behaviors separately.

1.action_down

Case MotionEvent.ACTION_DOWN:if (mmenustate = = menu_closed) {//View view view from coordinates FINDC
          Hildviewunder (x, y);
          if (view = = null) {return false;
          //Get this view viewholder rvadapter.holder Holder = (rvadapter.holder) getchildviewholder (view);
          Get the view of the position mposition = Holder.getadapterposition ();
          Get the entire layout of this view mitemlayout = Holder.lllayout;
          Get the Delete button for this view tvdelete = Holder.tvdelete;
          The entire top button of this view tvtop = Holder.tvtop;

          Width of two buttons mmaxlength = tvdelete.getwidth () + tvtop.getwidth (); Set the Delete button to tap listening Tvdelete.setonclicklistener (new Onclicklistener () {@Override public void O
              Nclick (view view) {Mitemlayout.scrollto (0, 0);
              Mmenustate =menu_closed;
            Mlistener.onitemdelete (mposition);
          }
          }); //Set the top button to tap listening Tvtop.setonclicklistener (new Onclicklistener () {@Override public void onclic
              K (view view) {Mitemlayout.scrollto (0, 0);
              Mmenustate =menu_closed;
            Mlistener.onitemtop (mposition);
          }
          }); If it is turned on, click on the other to turn off the current MENU.} else if (mmenustate = = Menu_open) {Mscroller.startscroll (MITEMLAYOUT.GETSCR
          OLLX (), 0,-mmaxlength, 0, 200);
          Invalidate ();
          Mmenustate = menu_closed;
        The click is invalid return false;
        else {return false;

 } break;

2.action_move

 case motionevent.action_move://COMPUTE offset int dx = mlastx-x;
        int dy = mlasty-y;

        The currently sliding x int scrollx = MITEMLAYOUT.GETSCROLLX ();
          if (math.abs (dx) > Math.Abs (dy)) {isitemmoving = true;
            The left boundary is always maintained if (scrollx + dx <= 0) {mitemlayout.scrollto (0, 0);
          Sliding invalid return false;
            Out of the right boundary is always persisted} else if (scrollx + dx >= mmaxlength) {mitemlayout.scrollto (mmaxlength, 0);
          Sliding invalid return false;
        }//menu with fingers moving Mitemlayout.scrollby (DX, 0);
        If the horizontal move distance is greater than 30 pixels, the Recyclerview does not slide up and down}else if (math.abs (dx) >) {return true;
          ////If the menu is open, it cannot slide up or down if (isitemmoving) {mlastx = x;
          Mlasty = y;
        return true;

} break; 

3.action_up

Case MOTIONEVENT.ACTION_UP:
        //When the finger raised to determine whether to click, still and have listener to click
        if (!isitemmoving &&!isdragging & & Mlistener!= null) {
          mlistener.onitemclick (mposition);
        }
        Isitemmoving = false;

        Wait a minute. The distance to move
        int deltax = 0;
        int upscrollx = MITEMLAYOUT.GETSCROLLX ();
        The sliding distance is larger than the 1/2menu length, or it will turn off
        if (upscrollx >= mmaxlength/2) {
          deltax = mmaxlength-upscrollx;
          Mmenustate = Menu_will_open;
        } else if (Upscrollx < MMAXLENGTH/2) {
          deltax =-upscrollx;
          Mmenustate = menu_will_closed;
        }
        Do you know why we don't assign mmenustate to Menu_open or menu_closed directly? Because there is time for sliding, we can change the state to complete
        mscroller.startscroll (upscrollx, 0, deltax, 0) when the slide is completed;
        Isstartscroll = true;
        Refresh Interface
        invalidate ();
        Break

And then you need to refresh the coordinates in the Ontouchevent method.

   Only update mlastx,mlasty data will be accurate
    mlastx = x;
    Mlasty = y;
    Return Super.ontouchevent (e);

Because we used startscroll, so we're rewriting the Computescroll method.

  public void Computescroll () {
    //To determine whether scroller completes sliding
    if (Mscroller.computescrolloffset ()) {
      Mitemlayout.scrollto (Mscroller.getcurrx (), Mscroller.getcurry ());
      This is very important
      invalidate ();
    Change the state if it has already been completed
    } else if (isstartscroll) {
      isstartscroll = false;
      if (mmenustate = = menu_will_closed) {
        mmenustate = menu_closed;
      }
      if (mmenustate = = Menu_will_open) {
        mmenustate = Menu_open
      ;
    }
  }}

* * Also to monitor whether the Recyclerview is sliding up and down

 @Override public
  void onscrollstatechanged (int state) {
    super.onscrollstatechanged);
    Whether to slide up and down
    isdragging = state = = scroll_state_dragging;
  }

Also set listener

Set listener public
  void Setonitemactionlistener (Onitemactionlistener onitemactionlistener) {
    This.mlistener = Onitemactionlistener;
  }

This listener is to be built by himself.

Public interface Onitemactionlistener {
  void onitemclick (int position);
  void onitemtop (int position);
  void Onitemdelete (int position);
}

The last is click, Top, delete the callback in the activity

Here only shows the callback implementation part, I use here the list is LinkedList, can add the data in the first place

Rv.setonitemactionlistener (New Onitemactionlistener () {
      //click
      @Override public
      void Onitemclick (int Position) {
        Toast.maketext (mainactivity.this, "click" +position,toast.length_short). Show ();
      Top
      @Override public
      void onitemtop (int position) {
        //Get current position content
        String temp =list.get (position);
        Remove this item
        list.remove (position);
        Add it to the first
        List.addfirst (temp);
        Update data source
        adapter.notifydatasetchanged ();
      }
      Deletes
      @Override public
      void onitemdelete (int position) {
        list.remove (position);
        Update data source
        adapter.notifydatasetchanged ();
      }
    });

Adapter and layout of the code is too simple I will not put out, you can go to the source code to see what

Effect chart

Source Address: Https://github.com/jkgeekJack/SlideLeftMenuRecyclerView

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.