ListView and Popupwindow of Android program development Slide Delete function from left to right _android

Source: Internet
Author: User
Tags abs

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.

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; privat
e 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); Android 6.0 Previous versions need SETBACKGROunddrawable (),//To achieve the function of vanishing 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 = (i
NT) ev.gety (); Switch (action) {case MotionEvent.ACTION_DOWN:isSliding = false; mxdown = x; mydown = y; mcurrentposition = Pointtopositi
On (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 (), location[1] +
Mcurrentview.getheight ()/2-MPOPUPWINDOWHEIGHT/2); Mdelbtn.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {mlistener.clickhappend (
Mcurrentposition);
Mpopupwindow.dismiss ();
}
}); Case MOTIONEVENT.ACTION_UP://Issliding If this is returned to false, then the Super.ontouchevent event will be executed later. The Abslistview Ontouchevent invokes the Ontouchup method, which is likely to execute in the Ontouchup method Performclick.run ()--> Performitemclick ()-->
Super.performitemclick//--> Monitemclicklistener.onitemclick, which eventually triggers 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 call the Setfocusable () method;

B, Android 6.0 prior version needs setbackgrounddrawable () (For specific reasons: Popupwindow use).

The above is a small set of the Android program to introduce the development of ListView and Popupwindow implementation of sliding delete function, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.