Android ListView To implement a simple example of a left-slip delete button for a fake iphone _android

Source: Internet
Author: User
Tags abs assert

Custom ListView is required. Let's make floatdellistview here.

Replication Ontouchevent method. As follows:

@Override public boolean ontouchevent (motionevent ev) {switch (ev.getaction ()) {case Motionevent.action_d 
        own:<br>//Get Pressed Entry view (child view) int childcount = Getchildcount (); 
        int[] listviewcoords = new int[2]; 
        Getlocationonscreen (listviewcoords); 
        int x = (int) ev.getrawx ()-listviewcoords[0]; 
        int y = (int) Ev.getrawy ()-listviewcoords[1]; 
          for (int i = 0; i < ChildCount i++) {downchild = Getchildat (i);//Rect Rect = new Rect (); 
          Assert downchild!= null; 
 
          Downchild.gethitrect (rect); 
 
          int childposition = Getpositionforview (Downchild); 
            if (Rect.contains (x, y)) {downx = Ev.getrawx (); 
 
            int downposition = childposition; 
            Velocitytracker = Velocitytracker.obtain (); 
            Assert velocitytracker!= null; 
            Velocitytracker.addmovement (EV); 
          Break } 
        } 
        Isswipe = false; 
      Break Case MotionEvent.ACTION_MOVE:velocityTracker.addMovement (EV);<br>//calculates horizontal and vertical movement speed Veloc 
        Itytracker.computecurrentvelocity (1000); 
        float Velocityx = Math.Abs (Velocitytracker.getxvelocity ()); 
float Velocityy = Math.Abs (Velocitytracker.getyvelocity ()); 
        <BR>//horizontal move distance float DeltaX = ev.getrawx ()-downx; 
        float Deltamode = Math.Abs (deltax); 
        if (DeltaX >) {//Right swipe (slippery) Isswipetoleft = false; 
        else if (DeltaX < -150) {//left swipe (left-hand) Isswipetoleft = true; }<br>//If the horizontal sliding distance is greater than 0 and the horizontal sliding rate is larger than the vertical, the horizontal slip if (Deltamode > 0 && velocityy < Velocityx 
          {<br>//The floatdelbuttonlayout here is a custom linearlayout. 
          ((floatdelbuttonlayout) downchild). Showdelbutton (EV, isswipetoleft); 
        Isswipe = true; 
      } break; Case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_UP:downChild.setSelected (FALSE); 
          if (isswipe) {isswipe = false; 
        return true; 
    } break; 
  return super.ontouchevent (EV); }

Floatdelbuttonlayou.java:

public class Floatdelbuttonlayout extends LinearLayout {<BR>//provides delete button interface private Ondellistener Dellistener; 
<BR>//Current view index in list, using private int index in Dellistener; <BR>//Right or left-slip? 
    <BR> Private Boolean isswipetoleft;<br> public void Setondellistener (Ondellistener listener, int i) { 
    Dellistener = listener; 
  index = i; 
  Public Floatdelbuttonlayout {Super (context, NULL); 
  Public Floatdelbuttonlayout (context, AttributeSet attrs) {Super (context, attrs, 0); Floatdelbuttonlayout (context, AttributeSet attrs, int defstyle) {Super (context, Attrs, defstyl 
  e); 
  <BR>//used to show or hide the delete button. 
    public void Showdelbutton (Motionevent ev, Boolean isswipetoleft) {this.isswipetoleft = Isswipetoleft; 
  Ontouchevent (EV); 
      Private Onclicklistener Clickdel = new Onclicklistener () {@Override public void OnClick (View v) { DelliStener.ondel (index); 
} 
  }; <BR>/**<br> * The event here is the,<br>/@Override public Bo that we have shown from the ontouchevent inside Floatdellistview. Olean Ontouchevent (motionevent event) {switch (motioneventcompat.getactionmasked (event)) {case MOTIONEVENT.A ction_move:<br>//Get Delete button object, layout must have id del_button button view = (Button) findviewbyi 
        D (R.id.del_button); 
        View.settext (R.string.del);<br>//Set button Marginlayoutparams, of course, can be made into a variety of actions, such as fade and so on display. 
        Marginlayoutparams layoutparams = (marginlayoutparams) view.getlayoutparams (); 
        Assert layoutparams!= null; 
          if (isswipetoleft) {view.setvisibility (view.visible); 
          View.setonclicklistener (Clickdel); 
        Layoutparams.leftmargin =-200; 
          else {view.setvisibility (view.gone); 
        Layoutparams.leftmargin = 0; 
        } view.setlayoutparams (Layoutparams); Invalidate (); 
        Break 
  Return Super.ontouchevent (event); 
  public interface Ondellistener {void Ondel (int i); } 
}

Above this Android ListView implementation of the iphone to implement the left-slip Delete button is a simple example of the small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling 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.