Sliding switching of multiple activity in Android Studio (ii)

Source: Internet
Author: User

1. Because all controls on the Android interface are generally on top of the layout control (such as Relativelayout), and the layout control can set the response to the touch event, the touch event can be intercepted by the setontouchlisten of the layout control. To do further processing.


2, about the interface sliding, involving gesture processing, and gesture (gesture) is a touch event of a higher level of events, you can pass the touch event into the Gesturedetector object for processing, and create Gesturedetector object, to create a Ongesturelistener object first, you can do gesture recognition in the onfling function of Ongesturelistener.


3, the specific process. Implement Ontouchlisten and Ongesturelisten two abstract classes, while implementing the abstract function.

(1) Inheriting abstract classes in intellisenseactivity



(2) Create layout control relativelayout with ID relativelayout



(3) Code writing

<span style= "FONT-SIZE:14PX;" >package Com.design.cjc.intellisense;import Android.content.intent;import Android.support.v7.app.actionbaractivity;import Android.os.bundle;import Android.view.gesturedetector;import Android.view.menu;import Android.view.menuitem;import Android.view.motionevent;import Android.view.View;import Android.widget.relativelayout;public class Intellisenseactivity extends Actionbaractivity implements    view.ontouchlistener,gesturedetector.ongesturelistener{private Relativelayout RL;    Private Gesturedetector GD;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_intellisense);        Rl= (relativelayout) Findviewbyid (r.id.relativelayout);        Rl.setontouchlistener (this);          Rl.setlongclickable (TRUE);    Very important gd=new Gesturedetector ((Gesturedetector.ongesturelistener) this); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {        Inflate the menu;        This adds items to the action bar if it is present.        Getmenuinflater (). Inflate (R.menu.menu_intellisense, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item);    } @Override public Boolean ondown (Motionevent e) {return false; } @Override public void onshowpress (Motionevent e) {} @Override the public boolean onsingletapup (Motionevent E    ) {return false; } @Override public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {RETurn false; } @Override public void onlongpress (Motionevent e) {} @Override the public boolean onfling (Motionevent E1, Mot Ionevent E2, float Velocityx, float velocityy) {//Gesture recognition//e1: Start information//e2: End information//velocityx:x direction move speed        degree//velocityy:y Direction movement Speed final int fling_min_distance=100;        final int fling_min_velocity=200; if (E1.getx ()-E2.getx () > Fling_min_distance && math.abs (Velocityx) > fling_min_velocity) {inten            T intent = new Intent (intellisenseactivity.this, pcctrlactivity.class);        StartActivity (Intent);    } return false;         } @Override public boolean OnTouch (View V, motionevent event) {return gd.ontouchevent (event);    Touch event handed to Gesturedetector to handle//return false; }}</span>


(3)pcctrlactivityIn doing withintellisenseactivity Similar treatment

Package Com.design.cjc.intellisense;import Android.content.intent;import android.support.v7.app.ActionBarActivity ; Import Android.os.bundle;import Android.view.gesturedetector;import Android.view.menu;import Android.view.menuitem;import Android.view.motionevent;import Android.view.view;import Android.widget.relativelayout;public class Pcctrlactivity extends Actionbaractivity implements View.ontouchlistener,    Gesturedetector.ongesturelistener {private relativelayout RL;    Private Gesturedetector GD;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_pcctrl);        Rl= (relativelayout) Findviewbyid (r.id.relativelayout);        Rl.setontouchlistener (this);          Rl.setlongclickable (TRUE);    Very important gd=new Gesturedetector ((Gesturedetector.ongesturelistener) this); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to The Action Bar if it is present.        Getmenuinflater (). Inflate (R.menu.menu_pcctrl, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        Noinspection simplifiableifstatement if (id = = r.id.action_settings) {return true;    } return super.onoptionsitemselected (item);    } @Override public Boolean ondown (Motionevent e) {return false; } @Override public void onshowpress (Motionevent e) {} @Override the public boolean onsingletapup (Motionevent E    ) {return false; } @Override public boolean onscroll (Motionevent E1, motionevent E2, float Distancex, float distancey) {retur    n false; } @Override public void ONLONGPRESS (Motionevent e) {} @Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float ve        LOCITYY) {final int fling_min_distance=100;        final int fling_min_velocity=200; if (E1.getx ()-E2.getx () > Fling_min_distance && math.abs (Velocityx) > fling_min_velocity) {inten            T intent = new Intent (Pcctrlactivity.this,intellisenseactivity.class);        StartActivity (Intent);    } return false;    } @Override public boolean OnTouch (View V, motionevent event) {return gd.ontouchevent (event); }}

Reference documents

References 1

References 2

Sliding switching of multiple activity in Android Studio (ii)

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.