The interface with listview cannot slide left or right through a gesture to switch the interface. Solution

Source: Internet
Author: User

The interface with listview cannot slide left or right through a gesture to switch the interface. Solution

Problem description:

This problem occurs when the ongesturelistener sliding window is switched. That is, when the interface contains listview, the sliding between the left and right of ongesturelistener is eaten by listview itself.

Problem Analysis:

In the Android system, event distribution and response are only performed per certain priority. If the activity contains listview, the system's ontouchevent event is first distributed to listview for processing. At this time, the listview onitemclicklistener listener will first respond to the ontouchevent event. As a result, gesturedetector cannot receive the ontouchevent event of the system.

Solution:

There are two solutions:

First, change the order of ontouchevent events distributed by the system. This method is relatively simple.

Type 2: Customize listview so that it supports the ongesturelistener of gesturedetector. This method is more complex than the first method.

The following describes the specific practices of the two methods.

First, change the order of ontouchevent events distributed by the system:

/*** Override this method to distribute touch events to gesturedetector preferentially to solve the problem that sliding listview cannot switch the screen, **/@ overridepublic Boolean dispatchtouchevent (motionevent eV) {// todo auto-generated method Stub this. gesturedetector. ontouchevent (EV); return Super. dispatchtouchevent (EV );}

Type 2: Customize the listview so that it supports the ongesturelistener of gesturedetector:

Step 1: Customize listview:

/*** Custom listview with 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 Stub this. context = context; gesturedetector = new gesturedetector (context, new gesture (context);} public gesturelist (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle); // todo auto-generated constructor Stub this. context = context; gesturedetector = new gesturedetector (context, new gesture (context);} public gesturelist (context) {super (context ); // todo auto-generated constructor Stub this. context = context; gesturedetector = new gesturedetector (context, new gesture (context) ;}@ override public Boolean ontouchevent (motionevent eV) {If (gesturedetector. ontouchevent (EV) return true; return Super. ontouchevent (EV );}}

Step 2: Create a gesture listener for the custom listview:

Public class gesture implements ongesturelistener {/** obtain the global flag **/INT flag = baseactivity. flag;/** number of activities to be switched **/INT length = baseactivity. myclass. length; @ suppresswarnings ("rawtypes")/** get the activity array **/class [] myclass = baseactivity. myclass; Context context; public gesture (context) {// todo auto-generated constructor Stub this. context = context ;}@ override public Boolean ondown (motionevent e) {// todo auto-generated method stub return false;} @ override public void onshowpress (motionevent E) {// todo auto-generated method stub} @ override public Boolean onsingletapup (motionevent e) {// todo auto-generated method stub return false ;} @ override public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey) {// todo auto-generated method stub return false;} @ override public 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) {// sliding left if (e1.getx ()-e2.getx ()> 50) {log. I ("Fling", "gesture: Sliding left"); If (++ flag> = length) {flag = length-1; // change baseactivity, let it know that the flag changes baseactivity. flag = flag; return true;} baseactivity. flag = flag; intent = new intent (context, myclass [flag]); // The activity started with this flag will not exist in the stack once it exits. setflags (intent. flag_activity_no_history | intent. flag_activity_reorder_to_front); // you need context to start activity context. startactivity (intent); Return true;} // right-sliding else if (e2.getx ()-e1.getx ()> 50) {log. I ("Fling", "gesture: right sliding"); If (-- flag <0) {flag = 0; // change the baseactivity so that the flag bit changes the baseactivity. flag = flag; return true;} baseactivity. flag = flag; intent = new intent (context, myclass [flag]); // The activity started with this flag will not exist in the stack once it exits. setflags (intent. flag_activity_no_history | intent. flag_activity_reorder_to_front); // you need context to start activity context. startactivity (intent); // system. exit (0); // exit the current activity return true ;}}

Step 3: reference the custom listview on the layout page:

<com.jph.custom.GestureList android:id="@+id/list"    android:layout_height="match_parent"    android:layout_width="wrap_content"    android:focusable="false"    /> 


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.