Today's project, the need to use a sliding screen to bring up an interface, through their own attempts, combined with the online method, the successful implementation.
The code is as follows
PackageCom.example.text;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.GestureDetector;ImportAndroid.view.GestureDetector.OnGestureListener;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;Importandroid.view.MotionEvent;ImportAndroid.widget.TextView; Public classMainactivityextendsActivityImplementsOngesturelistener {PrivateGesturedetector Mgesturedetector; PrivateTextView TV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (r.id.tv);//This if you write this directly, you will be prompted out of date. In addition, if two parameters are written in this, an error will occur. Cause I don't know
Mgesturedetector=NewGesturedetector (mainactivity. This, This); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (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 the parent activity in Androidmanifest.xml. intID =Item.getitemid (); if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); } @Override Public Booleanontouchevent (Motionevent event) {//TODO auto-generated Method StubAutomatically determine gestures by event, callback the corresponding methodreturnmgesturedetector.ontouchevent (event); }
Press the screen to trigger, the coordinates are printed
@Override Public BooleanOndown (motionevent arg0) {//TODO auto-generated Method StubTv.settext (Arg0.getx () + "" +arg0.gety ()); return false; }
When the screen is sliding, the print is the coordinates of the starting point and the end point.
@Override Public BooleanOnfling (motionevent arg0, Motionevent arg1,floatArg2,floatArg3) { //TODO auto-generated Method StubTv.settext ("arg1.getx () =" +arg1.getx () + "arg0.getx () =" +arg1.gety () + "arg2=" +arg2+ "arg3=" +arg3); return true; } @Override Public voidonlongpress (motionevent arg0) {//TODO auto-generated Method Stub} @Override Public BooleanOnscroll (motionevent arg0, Motionevent arg1,floatArg2,floatArg3) { //TODO auto-generated Method Stub return false; } @Override Public voidonshowpress (motionevent arg0) {//TODO auto-generated Method Stub} @Override Public BooleanOnsingletapup (motionevent arg0) {//TODO auto-generated Method Stub return false; }}
The steps to use this class are as follows
1. Define gesture class:private gesturedetector mgesturedetector;
2. Initialize class: This is the same as other controls to set the listener, there are many ways
1) Anonymous inner class:
Mgesturedetector =NewGesturedetector (mainactivity. This,NewOngesturelistener () {@Override Public BooleanOnsingletapup (motionevent arg0) {//TODO auto-generated Method Stub return false; } @Override Public voidonshowpress (motionevent arg0) {//TODO auto-generated Method Stub} @Override Public BooleanOnscroll (motionevent arg0, Motionevent arg1,floatArg2,floatArg3) { //TODO auto-generated Method Stub return false; } @Override Public voidonlongpress (motionevent arg0) {//TODO auto-generated Method Stub} @Override Public BooleanOnfling (motionevent arg0, Motionevent arg1,floatArg2,floatArg3) { //TODO auto-generated Method Stub return false; } @Override Public BooleanOndown (motionevent arg0) {//TODO auto-generated Method Stub return false; } });
2). Direct the main class to inherit:
Public class extends Implements ongesturelistener{}
3). Inner class
class Myongesturelistener implements ongesturelistener{}
3. Use this class in the Ontouchevent () event
@Override publicboolean ontouchevent (motionevent event) { // TODO auto-generated method stub // Auto-judge gesture via event, callback corresponding methods return mgesturedetector.ontouchevent (event); }
However, it is not clear now that the return false in the method is different from the return true.
Implementation of android-gesturedetector gesture by means of sliding screen