In a blink of an eye in 15, I hope everyone in 15 to rise to the peak of life
This blog is a combination of the previous ListView sliding Delete viewgroup Build slide control (fixed version) blog completed, first.
Actually, it's not complicated to realize.
1. Resolve Sliding conflicts
Because our custom sliding controls and the sliding events of the ListView itself produce various conflicts, we can customize the ListView and override the Onintercepttouchevent method.
Let's take a look at the distribution of Android events and call ViewGroup's Dispatchtouchevent method first when the user touches the screen .
And in the Dispathtouchevent method will call the Onintercepttouchevent method, the main purpose of this method is to intercept the user's operation, specific distribution mechanism details can see Guo Shen blog Android event distribution mechanism fully resolved, Take you from the point of view of the source of a thorough understanding (bottom).
We're going to know that if this method returns false, then the touch event is all done by child view and you can see the code.
/** * Determines whether to intercept events */public Boolean onintercepttouchevent (motionevent ev) {float lastx = ev.getx (); Float lasty = Ev.gety (); SWI TCH (Ev.getaction ()) {case MotionEvent.ACTION_DOWN:mFirstX = Lastx;mfirsty = Lasty;int motionposition = pointtoposition ( (int) mfirstx, (int) mfirsty); if (motionposition >= 0) {Currentitemview = Getchildat (motionposition- Getfirstvisibleposition ()); Myswipeitem = (Myswipeitem) Currentitemview.findviewbyid (R.id.myitem);} Break;case MotionEvent.ACTION_MOVE:float dx = lastx-mfirstx;float dy = lasty-mfirsty; LOG.D ("TAG", "DX:" + dx); if (Math.Abs (DX) >= 5 && math.abs (DY) >= 5) {return false;} if (Math.Abs (dx) < 5 && math.abs (dx) > 0) {myswipeitem.tooff ();} Break;default:break;} return super.onintercepttouchevent (EV);}
Here I was in the move when the judgment, if the condition satisfies us that the user is in the horizontal sliding operation, and in the down time to get to the ListView sub-view, when the move condition does not meet the beginning of the vertical slide when the control is closed.
2, using the callback interface
Our effect is that if you slide the control off the next time you click anywhere other than the button, the slide control will be closed, and we can use the interface callback to achieve this effect.
Public interface Onscrollstate {public void ScrollView ();p ublic void Scrollon (view view); public void Setonscrollstate (Onscrollstate onscrollstate) {this.monscrollstate = onscrollstate;}
Customizes an interface and then makes a judgment on touch.
@Overridepublic boolean ontouchevent (Motionevent event) {int scrollx = GETSCROLLX (); int x = (int) event.getx (); Switch (eve Nt.getaction ()) {case Motionevent.action_down: {if (!mscroller.isfinished ()) {mscroller.abortanimation ();} if (monscrollstate! = null) {Monscrollstate.scrollview ();} break;} Case Motionevent.action_move: {int deltax = x-lastx;//calculates whether the sliding end point is legal, prevents sliding out of bounds int newscrollx = scrollx-deltax;if (deltax! = 0) {if (Newscrollx < 0) {newscrollx = 0;} else if (Newscrollx > Mmaxdistancex) {newscrollx = Mmaxdistancex;} This.scrollto (NEWSCROLLX, 0);} break;} Case MOTIONEVENT.ACTION_UP: {int newscrollx = 0;//here to make a judgment, when the hand is released, it will automatically slide to both sides, to which direction to slide, depending on where the current position if (Scrollx > MMAXDISTANCEX/2) {newscrollx = Mmaxdistancex;} Slide slowly toward the end This.smoothscrollto (newscrollx, 0); if (monscrollstate! = null && NEWSCROLLX = = Mmaxdistancex) { Monscrollstate.scrollon (this);} Break;}} Lastx = X;return true;}
To invoke a control's Close method in main
@Overridepublic void ScrollView () {if (Mlastitem! = null) {Mlastitem.tooff ();}} @Overridepublic void Scrollon (view view) {Mlastitem = (Myswipeitem) view;}
3, Disadvantages
It is easy to realize the sliding effect is not .... But we will find our own ListView Onitemclick method does not work!! , because we have rewritten the Onintercepttouchevent method, but the TextView and button in our sliding controls can still be used as onclick, so there is also a sliding delete method for the ListView to swipe the delete effect from the High Imitation dialog list.
4, PostScript
Don't ask me why I do not write a perfect, I will not say because I can not write and make lazy .... Beginners can learn to learn about the Android distribution mechanism, but if you really want to use the project, I would recommend going to GitHub down a mature
Project Source
Android combined slide Control listview Swipe Delete