Android implements left-sliding exit activity (perfect encapsulation)

Source: Internet
Author: User
Tags abs exit touch

In Android apps, there are many ways to exit an activity, such as setting a return navigation key at the top and clicking on the return key to exit. Of course, perhaps now also noticed that many of the app has adopted a left-sliding exit, such as the micro-letter Chat interface exit, etc., are used to the left-sliding exit.

With the love of technology, I have recently studied this feature. Now let's talk about my thinking:

We all know that Android is a lot of time using the architecture of MVC, the separation of data from view: So is my idea. In the sliding exit, we generally feel that this is the logic inside the Java code, in fact, I know that an interface, there must be their own view, so my first step is also starting from view.

Step One: Define a parent container of your own, and let it inherit from a layout (LinearLayout, relativelayout can),

With the code below:

/**
* Custom can slide the relativelayout, similar to the slide removal page effect of iOS, when we want to use
* This feature requires that the top-level layout of the activity be set to Sildingfinishlayout,
* Then you need to call the Settouchview () method to set the view that you want to slide
*
* @author xiaanming
*
* @blog http://blog.csdn.net/xiaanming
*
*/
public class Sildingfinishlayout extends LinearLayout implements
Ontouchlistener {
/**
* Sildingfinishlayout layout of the parent layout
*/
Private ViewGroup Mparentview;
/**
* View that handles sliding logic
*/
Private View Touchview;
/**
* Minimum distance of slide
*/
private int mtouchslop;
/**
* Press the X coordinate of the point
*/
private int downx;
/**
* Press the y-coordinate of the point
*/
private int DownY;
/**
* Temporary storage of x coordinates
*/
private int tempx;
/**
* Sliding class
*/
Private Scroller Mscroller;
/**
* Width of Sildingfinishlayout
*/
private int viewwidth;
/**
* Whether the record is slipping
*/
Private Boolean issilding;


Private Onsildingfinishlistener Onsildingfinishlistener;
Private Boolean isfinish;


Public Sildingfinishlayout (context context, AttributeSet Attrs) {
This is (context, attrs, 0);
}


Public Sildingfinishlayout (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);


Mtouchslop = Viewconfiguration.get (context). Getscaledtouchslop ();
Mscroller = new Scroller (context);
}


@Override
protected void OnLayout (Boolean changed, int l, int t, int r, int b) {
Super.onlayout (changed, L, T, R, b);
if (changed) {
Gets the parent layout of the layout where Sildingfinishlayout resides
Mparentview = (viewgroup) this.getparent ();
Viewwidth = This.getwidth ();
}
}


/**
* Set Onsildingfinishlistener, Finish activity in Onsildingfinish () method
*
* @param onsildingfinishlistener
*/
public void Setonsildingfinishlistener (
Onsildingfinishlistener Onsildingfinishlistener) {
This.onsildingfinishlistener = Onsildingfinishlistener;
}


/**
* Set the Touch view
*
* @param Touchview
*/
public void Settouchview (View touchview) {
This.touchview = Touchview;
Touchview.setontouchlistener (this);
}


Public View Gettouchview () {
return touchview;
}


/**
* Rolling out the interface
*/
private void Scrollright () {
Final int delta = (viewwidth + MPARENTVIEW.GETSCROLLX ());
Call the Startscroll method to set some scrolling parameters, we call Scrollto in the Computescroll () method to scroll the item
Mscroller.startscroll (MPARENTVIEW.GETSCROLLX (), 0,-delta + 1, 0,
Math.Abs (delta));
Postinvalidate ();
}


/**
* Scroll to start position
*/
private void Scrollorigin () {
int delta = MPARENTVIEW.GETSCROLLX ();
Mscroller.startscroll (MPARENTVIEW.GETSCROLLX (), 0,-delta, 0,
Math.Abs (delta));
Postinvalidate ();
}


/**
* Touch view is Abslistview, such as the ListView, GridView and other subclasses
*
* @return
*/
Private Boolean Istouchonabslistview () {
Return Touchview instanceof Abslistview? True:false;
}


/**
* Touch view is ScrollView or its subclass
*
* @return
*/
Private Boolean Istouchonscrollview () {
Return Touchview instanceof ScrollView? True:false;
}


@Override
public boolean Ontouch (View V, motionevent event) {
Switch (event.getaction ()) {
Case Motionevent.action_down:
Downx = TEMPX = (int) event.getrawx ();
DownY = (int) Event.getrawy ();
Break
Case Motionevent.action_move:
int moveX = (int) event.getrawx ();
int deltax = Tempx-movex;
TEMPX = MoveX;
if (Math.Abs (MOVEX-DOWNX) > Mtouchslop
&& math.abs ((int) Event.getrawy ()-DownY) < Mtouchslop) {
Issilding = true;


If Touchview is Abslistview,
Then when the finger slides, cancel the item click event, or we slide along with the item click event to occur
if (Istouchonabslistview ()) {
Motionevent CancelEvent = Motionevent.obtain (event);
CancelEvent
. Setaction (Motionevent.action_cancel
| (Event.getactionindex () << motionevent.action_pointer_index_shift));
V.ontouchevent (CancelEvent);
}


}


if (movex-downx >= 0 && issilding) {
Mparentview.scrollby (deltax, 0);


Shielding in the course of the slide ListView ScrollView and other sliding events
if (Istouchonscrollview () | | Istouchonabslistview ()) {
return true;
}
}
Break
Case MOTIONEVENT.ACTION_UP:
Issilding = false;
if (MPARENTVIEW.GETSCROLLX () <=-viewwidth/2) {
Isfinish = true;
Scrollright ();
} else {
Scrollorigin ();
Isfinish = false;
}
Break
}


If the touch view is Abslistview or scrollview we're done with our own logic,
and give it to Abslistview, ScrollView himself to deal with his own logic.
if (Istouchonscrollview () | | Istouchonabslistview ()) {
Return V.ontouchevent (event);
}


The other case returns true directly
return true;
}


@Override
public void Computescroll () {
Scroller.computescrolloffset () returns True when Startscroll is invoked,
if (Mscroller.computescrolloffset ()) {
Mparentview.scrollto (Mscroller.getcurrx (), Mscroller.getcurry ());
Postinvalidate ();


if (mscroller.isfinished ()) {


if (Onsildingfinishlistener!= null && isfinish) {
Onsildingfinishlistener.onsildingfinish ();
}
}
}
}


Public interface Onsildingfinishlistener {
public void Onsildingfinish ();
}


}

Now your own parent container has been, how to use it?

Open our layout layout file, reference the parent container at the outermost level, and add the ID;

In the OnCreate () method of activity

Sildingfinishlayout msildingfinishlayout = (sildingfinishlayout) Findviewbyid (r.id.timed_task_sildingfinishlayout) ;
Msildingfinishlayout
. Setonsildingfinishlistener (New Onsildingfinishlistener () {


@Override
public void Onsildingfinish () {
TimedTaskActivity.this.finish ();
}
});
Msildingfinishlayout.settouchview (Mlistview);//Bind a control, here I use the layout inside the definition listview,

By now, this feature has been implemented

More details can be referred to blog http://blog.csdn.net/xiaanming

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.