Android Implementation Sliding Delete operation (Popupwindow) _android

Source: Internet
Author: User
Tags abs

Refer to Android imitation Tencent QQ implementation of sliding Delete this article to learn, the article implementation of the function is: on the ListView item from right to left, the deletion button appears, click the Delete button to delete the item, the effect

After reading the article, it's not necessary to rewrite the dispatchtouchevent () and Ontouchevent () two methods, just rewrite the ontouchevent. This makes some adjustments to the code:

public class Mylistview extends ListView {private static final String TAG = "Mylistview";
 private int mtouchslop;
 private int mxdown;
 private int mydown;
 private int mcurrentposition;
 Private View Mcurrentview;
 Private Popupwindow Mpopupwindow;
 Private Layoutinflater Minflater;
 Private Boolean issliding = false;
 Provides a callback interface for the delete button private Delbuttonclicklistener mlistener;
 Private Button mdelbtn;
 private int mpopupwindowheight;

 private int mpopupwindowwidth;
  Public Mylistview (context, AttributeSet attrs) {Super (context, attrs);
  Minflater = Layoutinflater.from (context);

  Mtouchslop = Viewconfiguration.get (context). Getscaledtouchslop ();
  View view = Minflater.inflate (r.layout.delete_btn, NULL);
  MDELBTN = (Button) View.findviewbyid (R.ID.ID_ITEM_BTN); Mpopupwindow = new Popupwindow (view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT
  );
  Setfocusable (TRUE) is required if you need to make it disappear by clicking somewhere other than Popupwindow. Mpopupwindow.setfocusable (true);
  The previous version of Android 6.0 needs to be setbackgrounddrawable (),//To achieve the function of disappearing by clicking Popupwindow.
  Mpopupwindow.setbackgrounddrawable (new colordrawable (0));
  Call the lower measure first, or you will not get the width and height mpopupwindow.getcontentview (). Measure (0, 0);
  Mpopupwindowheight = Mpopupwindow.getcontentview (). Getmeasuredheight ();
 Mpopupwindowwidth = Mpopupwindow.getcontentview (). Getmeasuredwidth ();
  @Override public boolean ontouchevent (motionevent ev) {int action = ev.getaction ();
  int x = (int) ev.getx ();

  int y = (int) ev.gety ();
    Switch (action) {case MotionEvent.ACTION_DOWN:isSliding = false;
    Mxdown = x;
    Mydown = y;
    Mcurrentposition = Pointtoposition (Mxdown, Mydown);
    View view = Getchildat (Mcurrentposition-getfirstvisibleposition ());
    Mcurrentview = view;
   Break
    Case MotionEvent.ACTION_MOVE:int dx = x-mxdown;

    int dy = Y-mydown;

    LOG.D (TAG, "mtouchslop =" + Mtouchslop + ", dx =" + dx + ", dy =" + dy); if (Mxdown > x && math.abs (dx) > Mtouchslop && math.abs (dy) < Mtouchslop) {LOG.D (TAG, "issliding");
     Issliding = true;
     int[] location = new INT[2];
     Mcurrentview.getlocationonscreen (location);
     Mpopupwindow.setanimationstyle (R.style.popwindow_delete_btn_anim_style);
     Mpopupwindow.update ();
     LOG.D (TAG, "Height:" + mcurrentview.getheight () + "," + mpopupwindow.getheight ()); Mpopupwindow.showatlocation (Mcurrentview, gravity.no_gravity, location[0] + mcurrentview.getwidth (), Locatio
     N[1] + mcurrentview.getheight ()/2-MPOPUPWINDOWHEIGHT/2); Mdelbtn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {mlistener.cl
       Ickhappend (mcurrentposition);
      Mpopupwindow.dismiss ();
    }
     }); Case MOTIONEVENT.ACTION_UP://Issliding If this is returned to false, then the Super.ontouchevent event will be executed later,//and Abslistview Ontoucheven T calls the Ontouchup method, it is possible to execute//in the Ontouchup Method Performclick.run ()--> Performitemclick ()--> Super.performitemclick//--> Monitemclicklistener.onitemclick, this will eventually trigger the item's click.
    Therefore, this is still the state where issliding is true, and issliding is false in the Action_down event, and/or after all, each event starts with Action_down.
  Issliding = false;
  } if (issliding) {return true;
 return super.ontouchevent (EV);
 public void Setdelbuttonclicklistener (Delbuttonclicklistener listener) {Mlistener = listener;
 } interface delbuttonclicklistener{public void clickhappend (int position); 
 }
}

Learn from this example:
1, the listview item clicks the event the triggering process:
Custom ListView ontouchevent ()---call super.ontouchevent ()---> abslistview.ontouchevent ()---motionevent.action _UP---> abslistview.ontouchup ()
---(possible) call Performclick.run ()---> AbsListView.PerformClick.run ()---call Performitemclick ()---> Abslistview.performitemclick ()

---(possible) call Super.performitemclick ()---> Adapterview.performitemclick ()---monitemclicklistener.onitemclick--- > Onitemclicklistener.onitemclick ()
The item click event is completed in the Motionevent.action_up event, so that in the custom ListView ontouchevent (), direct return to MOTIONEVENT.ACTION_UP True consumes events instead of calling Super.ontouchevent. This avoids the conflict between the Delete button and the item click event.

2, popupwindow--by clicking Popupwindow outside of the place to make it disappear
A, need to invoke the Setfocusable () method (Popupwindow showatlocation ()--> createpopuplayoutparams ()--> computeflags ()--> Set flag_not_focusable);
B, Android 6.0 prior version needs setbackgrounddrawable () (For specific reasons: Popupwindow use).

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.