Slide left and right of the page with listview to switch the page.

Source: Internet
Author: User

Slide left and right with listview interface, switch interface

 

I believe you will encounter this problem when sliding the ongesturelistener switching window. That is, when the interface contains listview, the sliding between the left and right of ongesturelistener is eaten by listview itself.

Looking at the API help documentation and some of my own understandings, I decided to rewrite the listview to solve this headache.

 

Test source code:

Http://download.csdn.net/detail/wanli_smile/4182584 (recommended)

Http://download.csdn.net/detail/wanli_smile/4178852

 

Start: Create a baseature interface to implement gesture sliding switching, so that other activities can inherit it, so that other activities can inherit its sliding switching function to improve code reuse.

The following is baseactivity:

Package COM. gesturetest; import android. app. activity; import android. content. intent; import android. util. log; import android. view. gesturedetector; import android. view. gesturedetector. ongesturelistener; import android. view. motionevent;/***** @ author liwan * base classes of all activities. All inherited activities have the sliding function. */public class baseactivity extends activity implements ongesturelistener {/*** gesture listener */gesturedetector = new gesturedetector (this ); /*** flag */public static int flag = 0; @ suppresswarnings ("rawtypes ") /*** all switched activities */static class [] myclass = new class [] {testactivity. class, gesturetestactivity. class}; @ overridepublic Boolean ontouchevent (motionevent event) {// todo auto-generated method stubreturn gesturedetector. ontouchevent (event) ;}@ overridepublic Boolean ondown (motionevent e) {// todo auto-generated method stubreturn false;} @ overridepublic void onshowpress (motionevent E) {// todo auto-generated method stub} @ overridepublic Boolean onsingletapup (motionevent e) {// todo auto-generated method stubreturn false;} @ overridepublic Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey) {// todo auto-generated method stubreturn false;} @ overridepublic void onlongpress (motionevent E) {// todo auto-generated method stub} @ override/*** processing of sliding events */Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {log. I ("Fling", "baseactivity"); // sliding left if (e1.getx ()-e2.getx ()> 80 | e1.gety ()-e2.gety ()> 80) {flag = (-- flag + myclass. length) % myclass. length; intent = new intent (getbasecontext (), myclass [flag]); log. D ("flag", flag + ""); startactivity (intent); Return true;} // right-sliding else if (e1.getx ()-e2.getx () <-80 | e1.gety ()-e2.gety () <-80) {log. I ("Fling", "baseactivity"); flag = ++ flag % myclass. length; intent = new intent (getbasecontext (), myclass [flag]); startactivity (intent); log. D ("flag", flag + ""); Return true;} return true ;}}

It inherits two of its activities. They are as follows:

Gesturetestactivity introduces our custom gesturelist

GestureTestActivity

Package COM. gesturetest; import android. OS. bundle;/***** @ author liwan * Here gesturelist is in XML Layout */public class gesturetestactivity extends baseactivity {/*** custom listview */gesturelist gesturelistview; /*** custom adapter */mybaseadapter; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mybaseadapter = new mybaseadapter (this); gesturelistview = (gesturelist) This. findviewbyid (R. id. list1); gesturelistview. setadapter (mybaseadapter );}}
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <textview Android: layout_width = "fill_parent" Android: layout_height = "150dp" Android: text = "the first activity. Here, sliding is the gesture processing of calling the activity, slide call the list's own gesture in listview "/> <COM. gesturetest. gesturelist Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: Id = "@ + ID/list1"> </COM. gesturetest. gesturelist> </linearlayout>

The code for testactivity is as follows. It references gesturelist, but it is introduced using addview.

Package COM. gesturetest; import android. OS. bundle; import android. widget. linearlayout;/*** here, gesturelist is introduced by code, no layout in XML * @ author liwan **/public class testactivity extends baseactivity {/*** custom listview */gesturelist gesturelistview; /*** custom adapter */mybaseadapter; linearlayout; @ overrideprotected void oncreate (bundle savedinstancestate) {// todo auto-generated method stubsuper. oncreate (savedinstancestate); setcontentview (R. layout. test); linearlayout = (linearlayout) This. findviewbyid (R. id. li); gesturelistview = new gesturelist (testactivity. this); mybaseadapter = new mybaseadapter (testactivity. this); gesturelistview. setadapter (mybaseadapter); linearlayout. addview (gesturelistview );}}

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical" Android: id = "@ + ID/Li"> <textview Android: layout_width = "fill_parent" Android: layout_height = "100dp" Android: text = "second activity, here, slide is used to call activity gesture processing. In listview, slide calls the list's own gesture "/> </linearlayout>

Let's talk about the core, that is, our gesturelist. Thanks to its rewriting, it can also capture gesture events. In the ontouchevent method, the processing is handed over to the gesture listener.

Gesturelist is used in the XML layout. By default, gesturelist (context, attributeset attrs) is called ). You must override this constructor.

The Code is as follows:

Package COM. gesturetest; import android. content. context; import android. util. attributeset; import android. util. log; import android. view. gesturedetector; import android. view. motionevent; import android. widget. listview;/***** @ author liwan * Custom listview with a gesture */class gesturelist extends listview {int flag = baseactivity. flag; Context context; gesturedetector;/*** use gesturelist in the XML layout, by default, this constructor * @ Param context * @ Param attrs */Public gesturelist (context, attributeset attrs) {super (context, attrs ); // todo auto-generated constructor stubthis. context = context; gesturedetector = new gesturedetector (context, new gesture (context); log. D ("12", "2");} public gesturelist (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle ); // todo auto-generated constructor stubthis. context = context; gesturedetector = new gesturedetector (context, new gesture (context); log. D ("12", "3");} public gesturelist (context) {super (context); // todo auto-generated constructor stubthis. context = context; gesturedetector = new gesturedetector (context, new gesture (context); log. D ("12", "1") ;}@ overridepublic Boolean ontouchevent (motionevent eV) {If (gesturedetector. ontouchevent (EV) return true; return Super. ontouchevent (EV );}}

Package COM. gesturetest; import android. content. context; import android. content. intent; import android. util. log; import android. view. gesturedetector. ongesturelistener; import android. view. motionevent; public class gesture implements ongesturelistener {/*** get the global flag */INT flag = baseactivity. flag; @ suppresswarnings ("rawtypes")/*** get the activity array */class [] myclass = baseactivity. myclass; Context context; public gesture (context) {// todo auto-generated constructor stubthis. context = context ;}@ overridepublic Boolean ondown (motionevent e) {// todo auto-generated method stubreturn false;} @ overridepublic void onshowpress (motionevent E) {// todo auto-generated method stub} @ overridepublic Boolean onsingletapup (motionevent e) {// todo auto-generated method stubreturn false;} @ overridepublic Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey) {// todo auto-generated method stubreturn false;} @ overridepublic void onlongpress (motionevent E) {// todo auto-generated method stub} @ override/*** processing of sliding events */Public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {log. I ("Fling", "baseactivity"); // sliding left if (e1.getx ()-e2.getx ()> 80) {flag = (-- flag + myclass. length) % myclass. length; // change the baseactivity so that the flag changes the baseactivity. flag = flag; intent = new intent (context, myclass [flag]); // you need context to start activity context. startactivity (intent); Return true;} // right-sliding else if (e1.getx ()-e2.getx () <-80) {log. I ("Fling", "baseactivity"); flag = ++ flag % myclass. length; // change the baseactivity so that the flag changes the baseactivity. flag = flag; intent = new intent (context, myclass [flag]); // you need context to start activity context. startactivity (intent); Return true;} return true ;}}

QQ chat group for Android and other smart machines is recommended: 187651345

You can check the code on it. It's actually quite simple.

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.