Android Imitation Micro-trust dialog list sliding Delete effect _android

Source: Internet
Author: User
Tags abs gety

Micro-Letter Dialog list Sliding Delete effect is very good, for reference to the GitHub on the Swipelistview (Project address: Https://github.com/likebamboo/SwipeListView), on which a number of refactoring, Finally realized the Micro-letter dialog list sliding Delete effect.

Implementation principle
1. Through the listview pointtoposition (int x, int y) to get the pressed position and then through android.view.ViewGroup.getChildAt (position) To get the sliding object Swipeview
2. Calculate the distance to slide in ontouchevent, call Swipeview.scrollto.

Operation effect is as follows

Here is the core part of the Swipelistview code:

Package com.fxsky.swipelist.widget;
Import Android.annotation.SuppressLint;
Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;

Import Android.widget.ListView;

Import COM.FXSKY.SWIPELIST.R;

 public class Swipelistview extends ListView {private Boolean mishorizontal;

 Private View Mpreitemview;

 Private View Mcurrentitemview;

 private float mfirstx;

 private float mfirsty;

 private int mrightviewwidth;
 Private Boolean misinanimation = false;

 Private final int mduration = 100;

 Private final int mdurationstep = 10;

 Private Boolean Misshown;
 Public Swipelistview {This (context,null);
 Public Swipelistview (context, AttributeSet attrs) {This (context, attrs,0);
 
 Public Swipelistview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); TYpedarray Mtypedarray = context.obtainstyledattributes (Attrs, R.styleable.swipelistviewstyle); 
 
 Gets the custom property and default value Mrightviewwidth = (int) mtypedarray.getdimension (r.styleable.swipelistviewstyle_right_width, 200); 
 Mtypedarray.recycle (); /** * return True and deliver to ListView. return false and deliver to the child.
 If * Move, return true */@Override public boolean onintercepttouchevent (motionevent ev) {float lastx = Ev.getx ();
 float lasty = ev.gety ();
 Switch (ev.getaction ()) {case MotionEvent.ACTION_DOWN:mIsHorizontal = null;
 System.out.println ("onintercepttouchevent----->action_down");
 MFIRSTX = LASTX;
 Mfirsty = lasty;

 int motionposition = pointtoposition ((int) mfirstx, (int) mfirsty);
  if (motionposition >= 0) {View Currentitemview = Getchildat (Motionposition-getfirstvisibleposition ());
  Mpreitemview = Mcurrentitemview;
 Mcurrentitemview = Currentitemview;

 } break;
 Case MotionEvent.ACTION_MOVE:float dx = LASTX-MFIRSTX; float dy = Lasty-mfiRsty;
 if (math.abs (DX) >= 5 && math.abs (DY) >= 5) {return true;

 } break; Case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:System.out.println ("onintercepttouchevent----->
 Action_up "); if (Misshown && (mpreitemview!= Mcurrentitemview | | ishitcuritemleft (LASTX))) {System.out.println ("1---> Hi
  Ddenright "); /** * Situation One: * <p> * The right layout of an item has been shown, * <p> * then click on any item, then the right layout shows the item to hide its right layout * * * hiddenright
 (Mpreitemview);
 } break;
 return super.onintercepttouchevent (EV);
 Private Boolean Ishitcuritemleft (float x) {return x < GetWidth ()-mrightviewwidth; /** * @param dx * @param dy * @return Judge if can judge scroll direction * * Private Boolean Judgescrolldirection (float dx, float dy)

 {Boolean Canjudge = true;
 if (math.abs (dx) > && math.abs (dx) > 2 * math.abs (dy)) {mishorizontal = true;
 System.out.println ("mishorizontal---->" + mishorizontal); else if (math.abs(dy) > && math.abs (DY) > 2 * math.abs (dx)) {mishorizontal = false;
 System.out.println ("mishorizontal---->" + mishorizontal);
 else {Canjudge = false;
 return Canjudge; /** * return False, can ' t move any direction. return true, cant ' t move * vertical, can move horizontal.
 return super.ontouchevent (EV), can move * both.
 * * @Override public boolean ontouchevent (motionevent ev) {float lastx = Ev.getx ();

 float lasty = ev.gety ();
 Switch (ev.getaction ()) {Case MotionEvent.ACTION_DOWN:System.out.println ("---->action_down");

 Break
 Case MotionEvent.ACTION_MOVE:float dx = LASTX-MFIRSTX;

 float dy = lasty-mfirsty;
  Confirm is scroll direction if (Mishorizontal = = null) {if (!judgescrolldirection (dx, dy)) {break; } if (Mishorizontal) {if (Misshown && mpreitemview!= mcurrentitemview) {System.out.println ("2---> H
  Iddenright "); /** * Situation II: * <p> * The right layout of an item has been shown, * <p> * This time to slide around the otherOne item, that right layout shows the item hide its right layout * <p> * slide to the left only triggers the case, sliding right also triggers the situation five/hiddenright (Mpreitemview);
  } if (misshown && Mpreitemview = = Mcurrentitemview) {dx = dx-mrightviewwidth;
  System.out.println ("======DX" + dx); }//Can ' t move beyond boundary if (DX < 0 && dx >-mrightviewwidth) {mcurrentitemview.scrollto (int
  ) (-DX), 0);
 return true;
  else {if (Misshown) {System.out.println ("3---> hiddenright"); /** * Situation Three: * <p> * The right layout of an item has been shown, * <p> * This time scrolling up and down ListView, then the right layout shows the item to hide its right layout * * * Hiddenrigh
  T (Mpreitemview);

 }} break;
 Case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:System.out.println ("============action_up");
 Clearpressedstate ();
  if (Misshown) {System.out.println ("4---> hiddenright"); /** * Situation Four: * <p> * The right layout of an item has been shown, * <p> * This time to slide around the current item, the right layout shows the item to hide its right layout * * * * Hiddenright (
 Mpreitemview); } if (Mishorizontal!= null && Mishorizontal) {if (Mfirstx-lastx > Mrightviewwidth/2) {showright (Mcurrentitemview);
  else {System.out.println ("5---> hiddenright");
  /** * Situation Five: * <p> * Slide one item to the right, and the sliding distance is more than half the width of the right view, hidden.
  * * Hiddenright (Mcurrentitemview);
 return true;
 } break;
 return super.ontouchevent (EV); } private void Clearpressedstate () {//TODO current item is still has background, issue mcurrentitemview.setpressed (f
 Alse);
 Setpressed (FALSE);
 Refreshdrawablestate ();
 Invalidate ();

 private void Showright (view view) {System.out.println ("=========showright");
 Message msg = new Movehandler (). Obtainmessage ();
 Msg.obj = view;
 MSG.ARG1 = View.getscrollx ();
 MSG.ARG2 = Mrightviewwidth;

 Msg.sendtotarget ();
 Misshown = true;
 private void Hiddenright (view view) {System.out.println ("=========hiddenright");
 if (Mcurrentitemview = = null) {return;
 Message msg = new Movehandler (). Obtainmessage ();//msg.obj = view; MSG.ARG1 = View.getSCROLLX ();

 MSG.ARG2 = 0;

 Msg.sendtotarget ();
 Misshown = false;  /** * Show or hide right layout animation */@SuppressLint ("Handlerleak") class Movehandler extends Handler {int

 stepx = 0;

 int FromX;

 int ToX;

 View view;

 Private Boolean misinanimation = false;
 private void Animatioover () {misinanimation = false;
 stepx = 0;
 @Override public void Handlemessage (msg) {super.handlemessage);
 if (stepx = = 0) {if (misinanimation) {return;
 } misinanimation = true;
 View = (view) msg.obj;
 FromX = MSG.ARG1;
 ToX = MSG.ARG2;
 STEPX = (int) (TOX-FROMX) * mdurationstep * 1.0/mduration);
 if (stepx < 0 && stepx >-1) {stepx =-1;
 else if (stepx > 0 && stepx < 1) {stepx = 1;
  } if (Math.Abs (TOX-FROMX) < ten) {View.scrollto (ToX, 0);
  Animatioover ();
 Return
 } FromX + = stepx; Boolean islaststep = (stepx > 0 && fromX > ToX) | |
 (stepx < 0 && FromX < ToX); if (islastStep) {FromX = ToX;
 } view.scrollto (FromX, 0);

 Invalidate ();
 if (!islaststep) {this.sendemptymessagedelayed (0, Mdurationstep);
 else {animatioover ();
 }} public int getrightviewwidth () {return mrightviewwidth;
 The public void setrightviewwidth (int mrightviewwidth) {this.mrightviewwidth = Mrightviewwidth;
 }
}

Demo Download Address: Http://xiazai.jb51.net/201608/yuanma/SwipeListView (jb51.net). rar

Demo in the Swipeadapter source code in one place due to careless write wrong, will cause the downward slide when the array out of bounds anomaly, now corrected as follows:

@Override public
 int GetCount () {
//;
 return Data.size ();
 }

This article has been organized into the "Android micro-credit Development tutorial Summary," Welcome to learn to read.

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.