Android Imitation micro-letter list Sliding Delete How to implement slide list swipelistview_android

Source: Internet
Author: User
Tags abs gety

Next, this article is mainly about how to implement sliding list Swipelistview.

The previous article completed the sliding control Swipeitemview, the control is a custom viewgroup, as a list of items, to provide a list of ways to allow this swipeitemview to slide its view content, while sliding process will have a smooth animation effect. And the Swipelistview of this article is the concrete realization of this list. Of course, this swipelistview inherits from ListView, and in order to achieve the functionality we need, the emphasis is on rewriting the ListView ontouchevent () and Onintercepttouchevent () method. First say Ontouchevent ():

@Override public boolean ontouchevent (motionevent ev) {//if user had not set MSWIPEITEMVIEWID, not handle any TOU

    Ch Event if (Mswipeitemviewid = 1) return super.ontouchevent (EV); if (mcancelmotionevent && ev.getaction () = = Motionevent.action_down) {//ev.setaction (Motionevent.action_canc
      EL);
      LogUtil.Log ("Swipelistview.ontouchevent (), Cancel Action_down");

      Hideshowingitem ();
    return true; else if (mcancelmotionevent && ev.getaction () = = Motionevent.action_move) {if (MSWIPEITEMVIEW.GETCURRENTSCR
        OLLX () > 0) {mswipeitemview.computescroll ();
      Mswipeitemview.scrollby (-1, 0);
    return true;

      else if (mcancelmotionevent && ev.getaction () = = motionevent.action_up) {mcancelmotionevent = false;
    return true;
        Switch (ev.getaction ()) {case Motionevent.action_down: {LogUtil.Log ("Action_down"); if (Mtracker = = null) {Mtracker = Velocitytracker.obtain ();
        else {mtracker.clear ();
        } Mactiondownx = Ev.getx ();
        Mactiondowny = Ev.gety ();
        Mlastmotionx = Ev.getx ();
      Mlastmotiony = Ev.gety ();

      }break; Case Motionevent.action_move: {//if the scroll distance at x-axis or y-axis less than the//touch_slop, D o not handle the event Motionevent.action_move if (Math.Abs (Ev.getx ()-Mactiondownx) < Touch_slop | |

        Math.Abs (Ev.gety ()-Mactiondowny) < touch_slop) break;
        float CurX = Ev.getx ();
        float Cury = ev.gety ();
        int Distancex = (int) (MLASTMOTIONX-CURX);

        int distancey = (int) (Mlastmotiony-cury); if (mscrolldirection = = Direction_unknow && math.abs (Distancey) <= Math.Abs (Distancex)) MS
        Crolldirection = Direction_horizontal; else if (mscrolldirection = = Direction_unknow && math.abs (distancey) > MatH.abs (Distancex)) mscrolldirection = direction_vertical;
          If ListView is scrolling vertical, does not handle the touch event if (mscrolldirection = direction_vertical)

        Break
        int lastpos = pointtoposition ((int) mactiondownx, (int) mactiondowny);
        int firstvisibleitempos = Getfirstvisibleposition ()-Getheaderviewscount ();
        int factpos = Lastpos-firstvisibleitempos;
        Mitemview = Getchildat (Factpos);
          if (Mitemview!= null) {Mswipeitemview = (Swipeitemview) Mitemview.findviewbyid (MSWIPEITEMVIEWID); if (Mswipeitemview.getslidingview ()!= null && mswipeitemview.getscrollx () <= MS Wipeitemview.getslidingview (). GetWidth () && mswipeitemview.getscrollx () >= 0) {if (MS WIPEITEMVIEW.GETSCROLLX () + Distancex > Mswipeitemview.getslidingview (). GetWidth () Dista Ncex = Mswipeitemview.getsliDingview (). GetWidth ()-MSWIPEITEMVIEW.GETSCROLLX ();

            else if (MSWIPEITEMVIEW.GETSCROLLX () + Distancex < 0) Distancex =-MSWIPEITEMVIEW.GETSCROLLX ();
          Mswipeitemview.scrollby (Distancex, 0);

          } Mlastshowingpos = Lastpos;
        Ev.setaction (Motionevent.action_cancel);
        } Mlastmotionx = CurX;
      Mlastmotiony = Cury;

      }break;
        Case MOTIONEVENT.ACTION_UP: {LogUtil.Log ("action_up");
          if (Mtracker!= null) {mtracker.clear ();
          Mtracker.recycle ();
        Mtracker = null;

        }//reset the mscrolldirection to direction_unknow mscrolldirection = Direction_unknow;

        Reset the mcancelmotionevent to false mcancelmotionevent = false;
      Ensure if the showing item need Open or hide if (mlastshowingpos!=-1) ensureifitemopenorhide ();

      }break;
  Case Motionevent.action_cancel: {      Hideshowingitem ();
    }break;
  return super.ontouchevent (EV);

 }

The above code, first analyzes the user to slide an item operation, this operation starts with Action_down, a series of action_move, to action_up as the end, so, in the Action_down incident, We first record the first event position Mactiondownx and Mactiondowny, and then action_move the incident, we first have to judge, this judgment is two steps, the first step, judge this action_move event, The position of the current event CurX and Cury whether the distance recorded on the x axis and on the y-axis and Action_down has exceeded the Touch_slop value, which is the threshold that Android uses to determine if a slide should be made, and the second step, We want to further determine whether the user is vertically sliding the entire list or sliding an item, where the logical judgment on the simple point of processing, if more than the touch_slop threshold, the x-axis distance greater than the Y axis, we think that the user is left and right to slide a single item, Conversely, it is the vertical slide of the entire list, where we use three states to differentiate, direction_unknow that the current sliding operation has not been judged left or right or vertical sliding, Direction_horizontal said that the current sliding operation to determine the right and left sliding, Direction_vertical is determined to be a longitudinal slide, once the sliding operation is determined, before action_up processing, we think that the user is in the same direction of the slide; action_up event, the reset sliding operation state is Direction_ Unknow so that the next decision, and this ACTION_UP event processing, if the current slide the position of the item Mlastshowingpos is not 1, then the current is a sliding operation, this time think about it, The user may have left the screen without a complete slide of the item's state. At this point we should be able to determine if this sliding item should be completely open or closed, and the logical judgment here is that the item has slipped more than half its width, opening it completely, Otherwise, close it, Ensureifitemopenorhide () is the specific code for the following:

 private void Ensureifitemopenorhide () {if (Mlastshowingpos!=-1) {int
      Visibleitempos = Getfirstvisibleposition ()-Getheaderviewscount ();
      int factpos = Mlastshowingpos-firstvisibleitempos;
      Mitemview = Getchildat (Factpos);
        if (Mitemview!= null) {Mswipeitemview = (Swipeitemview) Mitemview.findviewbyid (MSWIPEITEMVIEWID); if (Mswipeitemview.getslidingview ()!= null && mswipeitemview.getscrollx () >= mswipe
        Itemview.getslidingview (). GetWidth ()/2) {Openshowingitem ();
        else if (Mswipeitemview.getslidingview ()!= null) {Hideshowingitem (); }
      }
    }
  }

The logic of the first user sliding operation we're done with. Next is the second, why say the second time, the first time the user slipped open a single item, so that it is in the open state, once again touch the screen, this time we have to judge again, one, if Action_ The position of down occurs within the range of the button that the item slides out, indicating that the current user is clicking on the button, so that we do not do additional processing, directly to the list by default logic processing; second, if Action_ The position of down does not appear in the button range after item slide, how to indicate that the current user is only the other scope of the action list, here we close the currently open item, and cancel the subsequent touch event, here, we will intercept this Actioin_ Down event, you need to override the ListView Onintercepttouchevent () method with the following code:

public boolean onintercepttouchevent (motionevent ev) {
    //if user had not set MSWIPEITEMVIEWID, not handle no touch EV ENT
    if (mswipeitemviewid = = 1) return
      super.onintercepttouchevent (EV);

    if (Mlastshowingpos!=-1
        && ev.getaction () = = Motionevent.action_down
        &&!isclickchildview ( EV)) {
      LogUtil.Log ("Swipelistview.onintercepttouchevent (), intercept Action_down");
      Mcancelmotionevent = true;

      return true;
    } else if (Mlastshowingpos = = 1
        && ev.getaction () = = Motionevent.action_down) {return
      true;
    }

    return super.onintercepttouchevent (EV);
  }  

The above mcancelmotionevent is used in ontouchevent () to determine the need to cancel the follow-up touch events, during that time, how to determine whether the current Action_down event occurred in the range of the button, use the following code:

Private Boolean Isclickchildview (Motionevent event) {if (Mlastshowingpos!=-1) {int firstvisibleitempos = Get
      Firstvisibleposition ()-Getheaderviewscount ();
      int factpos = Mlastshowingpos-firstvisibleitempos;
      Mitemview = Getchildat (Factpos);
        if (Mitemview!= null) {Mswipeitemview = (Swipeitemview) Mitemview.findviewbyid (MSWIPEITEMVIEWID);
        View Slidingview = Mswipeitemview.getslidingview ();
          if (Slidingview!= null) {int[] slidingviewlocation = new int[2];

          Slidingview.getlocationonscreen (slidingviewlocation);
          int left = slidingviewlocation[0];
          int right = Slidingviewlocation[0] + slidingview.getwidth ();
          int top = slidingviewlocation[1];

          int bottom = slidingviewlocation[1] + slidingview.getheight (); Return (EVENT.GETRAWX () > Left && event.getrawx () < right && Event.getrawy () > Top && Event.getrawy () < botTom);
  }} return false;
 }

The rest of the

is how to turn on or off an item, the code is as follows:

private void Openshowingitem () {if (Mlastshowingpos!=-1) {int firstvisibleitempos = Getfirstvisibleposition (
      )-Getheaderviewscount ();
      int factpos = Mlastshowingpos-firstvisibleitempos;
      Mitemview = Getchildat (Factpos);
        if (Mitemview!= null) {Mswipeitemview = (Swipeitemview) Mitemview.findviewbyid (MSWIPEITEMVIEWID); if (Mswipeitemview.getslidingview ()!= null) mswipeitemview.scrolltowithanimation (MSWIPEITEMVIEW.G
      Etslidingview (). GetWidth (), 0); }} private void Hideshowingitem () {if (Mlastshowingpos!=-1) {int firstvisibleitempos = GETFIRSTVI
      Sibleposition ()-Getheaderviewscount ();
      int factpos = Mlastshowingpos-firstvisibleitempos;
      Mitemview = Getchildat (Factpos);
        if (Mitemview!= null) {Mswipeitemview = (Swipeitemview) Mitemview.findviewbyid (MSWIPEITEMVIEWID);
      Mswipeitemview.scrolltowithanimation (0, 0); } MLASTSHOWINGPOS =-1;


 }
  }

The Scrolltowithanimation () method above is a way to move the item and animate it in the previous blog.

In this way, the whole implementation of the Imitation micro-letter sliding delete operation is explained, the specific details can view the project source code, I have uploaded to my GitHub home page: https://github.com/YoungLeeForeverBoy/ Slidinglistviewplus.

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.