Recommended reading:
First to share, sideslip Delete, the layout is the previous item, and then there are two hidden buttons (TextView also can), and then we can slide to the left, and then show it, and then delete (delete key) to achieve monitoring, you can ha. All right, let's see how the code implements it.
First, same as before.
Custom view, initializing Viewdraghelper:
Package com.example.removesidepull;
Import Android.content.Context;
Import Android.support.v4.widget.ViewDragHelper;
Import Android.util.AttributeSet;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.widget.FrameLayout;
/** * Created by Roeland on 2016/2/2. * An understanding of programming fun small white, I hope I can go on this road very far, but also hope that they learn the knowledge can help more people, sharing is learning a fun * qq:1069584784 * csdn:http://blog.csdn.net/ Wuyinlei */public class Swipelayout extends Framelayout {private Viewdraghelper mdraghelper; Context) {This (context, null);} public swipelayout (context, AttributeSet attrs) {This (context, attrs, 0);} pub Lic swipelayout (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);//First step initialization of Vie
Wdraghelper Mdraghelper = Viewdraghelper.create (this, mcallback); Viewdraghelper.callback mcallback = new Viewdraghelper.callback () {@Override public boolean trycaptureview (View child,
int Pointerid) {//returns True return True;}}; }
And then we're going to deal with the interception event, which is to rewrite some onintercepttouchevent and ontouchevent methods that are not blocked by default:
/**
* Pass Touch Event
*
* @param ev
* @return
/@Override public
Boolean onintercepttouchevent ( Motionevent ev) {
//handed to Viewdraghelper to determine whether to intercept the event return
mdraghelper.shouldintercepttouchevent (EV);
@Override Public
Boolean ontouchevent (Motionevent event) {
try {
Mdraghelper.processtouchevent (event );
} catch (Exception e) {
e.printstacktrace ();
}
Returns true, which means to intercept the event return
true;
And then we'll rewrite the Clampviewpositionhorizontal method inside Viewdraghelper:
@Override public
int clampviewpositionhorizontal (View child, int left, int dx) {return left
;
}
Well, at this point, we're ready to slide, so let's take a look at the results:
Here we can see that it has been able to slide, well the next thing to do is to handle the sliding events and go to the right place (call me and delete are not visible at the beginning, there are only left-sliding display, right-sliding hide).
OK, let's get two view first:
/**
* When the XML is filled out
/
@Override
protected void Onfinishinflate () {
super.onfinishinflate ();
/**
* Post View * *
mbackview = getchildat (0);
/**
* Front View * * *
Mfrontview = Getchildat (1);
}
Get the width and height you want:
/**
* Here get wide and high
*
@param w *
@param h *
@param oldw
* @param OLDH *
*
@Override
protected void onsizechanged (int w, int h, int oldw, int oldh) {
super.onsizechanged (W, H, OLDW, OLDH);
/**
* Height * *
mheight = Mfrontview.getmeasuredheight ();
/**
* Width * *
mwidth = Mfrontview.getmeasuredwidth ();
/**
* Moving distance * *
mrange = Mbackview.getmeasuredwidth ();
}
Position of these two view:
/** * position * @param changed * @param left * @param top * @param right * @param bottom/@Override protected void onlayou T (Boolean changed, int left, int. top, int right, int bottom) {super.onlayout (changed, left, top, right, bottom); Layoutco
Ntent (FALSE); } private void Layoutcontent (Boolean isOpen) {//placed front view Rect frontrect = Computefrontviewrect (isOpen); mfrontview.layout
(Frontrect.left, Frontrect.top, Frontrect.right, Frontrect.bottom);
After placing view Rect Backrect = Computebackviewrect (Frontrect);
Mbackview.layout (Backrect.left,backrect.top,backrect.right,backrect.bottom);
Pre-placed view Bringchildtofront (Mfrontview); /** * We can put the front view equivalent to a rectangle * * @param frontrect * @return/private Rect Computebackviewrect (Rect frontrect) {int left =
Frontrect.right;
Return to New Rect (left, 0, left + mrange, 0 + mheight); Private Rect Computefrontviewrect (Boolean isOpen) {int left = 0; if (isOpen) {left =-mrange;} Return to New Rect (left,
0, left + mwidth, 0 + mheight); }
Of course this implementation, just can drag and drop the front view, because we did not pass the change DX, so as to implement drag-and-drop view when the view also followed (Viewdraghelper inside the method):
/**
* When the view position changes
* @param changedview changed view
* @param left
* @param top
* @param dx X-axis offset
* @pa Ram dy
/@Override public
void onviewpositionchanged (View changedview, int left, int top, int dx, int dy) {
super.onviewpositionchanged (Changedview, left, top, DX, dy);
Passing event, if it is a dragged front view, if
(Changedview = = Mfrontview) {
//offset this view's horizontal location by the specified a Mount of pixels.
That means my front view left the DX, then my rear view is also left-slip dx, right-sliding empathy
mbackview.offsetleftandright (dx);
} else if (Changedview = = Mbackview) {
//Drag is the post view word, the previous view is handled the same way
mfrontview.offsetleftandright (dx);
}
Compatible with the old version of
invalidate ();
}
Well, let's take a look at the effect this time:
Did you find the problem, that my front view would not be able to slide right (only allow left and back), and then realize the desired result. The following code is in the Clampviewpositionhorizontal () method:
Here's the logical drag-and-drop front view
if (child = = Mfrontview) {
if (left > 0) {return
0;
} else if (left <-mrange) {
return-mrange;
}
} Drag post View
else if (child = = Mbackview) {
if (left > Mwidth) {return
mwidth;
} else if (left < MW Idth-mrange) {return
mwidth-mrange;
}
}
Look at the effect chart:
OK, this time has been basically implemented, next to achieve the following sliding distance and speed "to determine whether to open and close:
/** *
Drag view released when * * *
@param releasedchild
* @param xvel
* @param yvel
* Override public
void onviewreleased (View releasedchild, float xvel, float yvel) {
if (xvel = 0 && Mfron Tview.getleft () <-mrange/2.0f) {
open ();
} else if (Xvel < 0) {
open ();
} else {close
() ;
}
}
/**
* Shut/public
void Close () {
utils.showtoast (GetContext (), "closed");
Layoutcontent (false);
Open the public
Void Open () {
//utils.showtoast (GetContext (), "open");
Layoutcontent (true);
OK, next you have the following smooth closing and opening:
public void Close () {Close
(true);
}
/** *
closed
*
@param issmooth
/public
void Close (Boolean issmooth) {
int finalleft = 0;
if (issmooth) {
//Start animation If return True indicates that the animation is not completed if
(Mdraghelper.smoothslideviewto (Mfrontview, Finalleft, 0)) {
viewcompat.postinvalidateonanimation (this);
}
else {
layoutcontent (false);
}
}
public void Open () {
open (true);
}
/** *
Open
*
@param issmooth
/public
Void Open (Boolean issmooth) {
int finalleft =- Mrange;
if (issmooth) {
//Start animation
if (Mdraghelper.smoothslideviewto (Mfrontview, Finalleft, 0)) {
Viewcompat.postinvalidateonanimation (this);
}
else {
layoutcontent (true);
}
}
/**
* Continuous animation * *
@Override public
void Computescroll () {
super.computescroll ();
This is a fixed
if (mdraghelper.continuesettling (True)) {
viewcompat.postinvalidateonanimation (this);
}
Let's look at the final effect:
OK, here we add some callbacks to make it easier to use externally when you can callback:
/** * Default state is off/private status status = Status.close; private Onswipelayoutlistener SWI
Pelayoutlistener; Public status GetStatus () {return status} public void SetStatus (status status) {This.status = status.} Public Onswipe Layoutlistener Getswipelayoutlistener () {return swipelayoutlistener.} public void Setswipelayoutlistener ( Onswipelayoutlistener swipelayoutlistener) {this.swipelayoutlistener = Swipelayoutlistener;}/** * Define three states */public enum Status {Close, Open, draging}/** * Define callback interface This is in our * * public interface Onswipelayoutlistener {/** * closed * * @param mswipe
Layout */void OnClose (Swipelayout mswipelayout);
/** * Open * * @param mswipelayout/void OnOpen (Swipelayout mswipelayout);
/** * Draw * @param mswipelayout/void ondraging (Swipelayout mswipelayout);
/** * to close/void Onstartclose (Swipelayout mswipelayout);
/** * to open/void Onstartopen (Swipelayout mswipelayout); }
Dispatchswipeevent () method (called in the Onviewpositionchanged () method)
protected void Dispatchswipeevent () {
//To determine if NULL if
(Swipelayoutlistener!= null) {
Swipelayoutlistener.ondraging (this);
}
Record the last state status
prestatus = status;
Update current status
= UpdateStatus ();
if (prestatus!= status && swipelayoutlistener!= null) {
if (status = = Status.close) {
swipelayoutliste Ner.onclose (this);
} else if (status = = Status.open) {
swipelayoutlistener.onopen (this);
} else if (status = = status.draging) {
if (Prestatus = = status.close) {
swipelayoutlistener.onstartopen (this);
} else if (Prestatus = Status.open) {
swipelayoutlistener.onstartclose (this);}}}
UpdateStatus () Method:
/**
* Update Status
*
* @return
/
private Status UpdateStatus () {
//Get the left position of the front view
int ieft = Mfrontview.getleft ();
if (left = = 0) {
//If the position is 0, it is the shutdown status return
status.close
} else if (left-hand =-mrange) {
//If the margin is the width of the rear view Negative value, the status is open return
status.open;
}
Other states are drag-and-drop return
status.draging;
OK, the event has basically been completed, this side pull deleted I will update to my project, and I hope that Android high imitation QQ6.0 sideslip Delete instance code to help everyone.