Android swipe right to close the page

Source: Internet
Author: User
Tags gety

Objective:

Use the simplest example to illustrate this problem.

1. Add the default layout layouts to the activity

2. Implement the logic of right-sliding off activity in custom layout

Directly on the code!

Customizing the Layout hffinishrelativelayout!

Package Com.huofar.widget;import Android.content.context;import Android.content.res.typedarray;import Android.support.v4.app.fragmentactivity;import Android.util.attributeset;import Android.view.MotionEvent;import Android.view.viewgroup;import android.widget.relativelayout;import com.huofar.util.logutil;/** * Created by Zhangxiwei on 14/11/25. */public class Hffinishrelativelayout extends relativelayout{private static final String TAG = Logutil.makelogtag (hffi    Nishrelativelayout.class);    public interface scrollleftfinishlistener{public void Finishpage ();    } fragmentactivity activity;    Private Scrollleftfinishlistener Scrollleftfinishlistener; public void Setscrollleftfinishlistener (Scrollleftfinishlistener scrollleftfinishlistener) {This.scrollLeftFinishL    Istener = Scrollleftfinishlistener;    }//sliding distance and coordinates private float xdistance, ydistance, XLAST, Ylast;    Public Hffinishrelativelayout (Context context) {super (context); } public HffiniShrelativelayout (context context, AttributeSet Attrs) {Super (context, attrs);        } public void Attachtoactivity (fragmentactivity activity) {this.activity = activity; TypedArray a = Activity.gettheme (). Obtainstyledattributes (New int[] {Android.        R.attr.windowbackground});        int background = A.getresourceid (0, 0);        A.recycle ();        ViewGroup decor = (viewgroup) Activity.getwindow (). Getdecorview ();        ViewGroup decorchild = (viewgroup) decor.getchildat (0);        Decorchild.setbackgroundresource (background);        Decor.removeview (Decorchild);        AddView (Decorchild);    Decor.addview (this);            } @Override public boolean onintercepttouchevent (Motionevent event) {switch (event.getaction ()) {            Case MotionEvent.ACTION_DOWN:break;    Case Motionevent.action_move:} return Super.onintercepttouchevent (event); }
 @Override 
    public boolean dispatchtouchevent (Motionevent event) {switch (event.getaction ()) {case Motionevent .                Action_down:xdistance = Ydistance = 0f;                XLAST = Event.getx ();                Ylast = Event.gety ();            Break                Case MotionEvent.ACTION_MOVE:final Float CurX = Event.getx ();                Final float CurY = event.gety ();                Xdistance + = Math.Abs (curx-xlast);                Ydistance + = Math.Abs (cury-ylast); if (CurX > XLAST && xdistance > Ydistance && xdistance >) {if (SCROLLLEFTF                        Inishlistener = null) {XLAST = CurX;                        Ylast = CurY;                        Scrollleftfinishlistener.finishpage ();                    return true;                }} XLAST = CurX;        Ylast = CurY; } return Super.dispatchtouchevent (event);    } @Override public boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case            MotionEvent.ACTION_DOWN:break;        Case MotionEvent.ACTION_MOVE:break; } <strong><span style= "color: #ff6666;" >return true;</span></strong>}}
Focus on the red bold field above.

I am dealing with each slide to the right to swipe the 300px to perform the close operation, the use of activity in the need to implement the pretext of direct finish is OK!

Calls in the activity:

Package Com.huofar.activity;import Android.os.bundle;import Android.os.handler;import Android.support.v4.app.fragmentactivity;import Android.view.layoutinflater;import Com.huofar.R;import com.huofar.widget.hffinishrelativelayout;/** * Created by Zhangxiwei on 14/11/25. */public class Hfbaseactivity extends Fragmentactivity implements Hffinishrelativelayout.scrollleftfinishlistener {PR    Ivate Boolean isfinishscrollleft;    Hffinishrelativelayout hffinishrelativelayout;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Isfinishscrollleft = true;                Delay in order to wait for a mask to return sliding accept parameter New Handler (). Postattime (New Runnable () {@Override public void run () {            Setfinishscrollleft (Isfinishscrollleft);    }},1000);        The public void Setfinishscrollleft (Boolean isfinishscrollleft) {this.isfinishscrollleft = Isfinishscrollleft;           if (isfinishscrollleft) { if (isfinishscrollleft) {hffinishrelativelayout = (hffinishrelativelayout) layoutinflater.from (this). Infla                TE (r.layout.activity_finish_base, null);                Hffinishrelativelayout.attachtoactivity (this);            Hffinishrelativelayout.setscrollleftfinishlistener (this);    }}} @Override public void Finishpage () {finish (); }}

All of the code is this, the delay of one second is to accept the isfinishscrollleft variable, I was in the existing project modification, is to some pages do not need to close the variable on the OK!. method is a bit stupid. Everyone has a good way to provide. Thanks first!


And then there is the headache, why do you do this:

Knowledge: 1.dispatchTouchEvent 2.onInterceptTouchEvent 3.onTouchEvent Pass-through event just give you a study address.

Http://www.cnblogs.com/sunzn/archive/2013/05/10/3064129.html

In addition to see the vaguely can write a demo of their own practice or a few more Google introduction to see, is the transmission of events, and then in Dispatchtouchevent accept. Then just close the line.

In fact, there is nothing to help children who want the function of shoes.

If you have any questions, you can ask them.


Android swipe right to close the page

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.