The problem of switching the interface between the left and right sides of the interface with ListView cannot be solved through the gesture. The listview gesture

Source: Internet
Author: User

The problem of switching the interface between the left and right sides of the interface with ListView cannot be solved through the gesture. The listview gesture

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 gestureDetector;/*** use GestureList in the xml layout, by default, this constructor * @ param context * @ param attrs */public GestureList (Context context, AttributeSet attrs) {super (context, attrs ); // TODO Auto-generated constructor stub this. context = context; gestureDetector = new GestureDetector (context, new Gesture (context);} public GestureList (Context 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 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 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 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 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"    /> 



Android problem: in an interface, the user can make a response through gesture judgment.

I don't know much about how android gets mouse points.
But you can give me some ideas.
Define an action variable
Records the movement tracks of touch points x1, y1, x2, y2, x3, y3 ......
Judge whether the vector between each two points is 0 if it is less than or equal to 45 degrees.
Greater than 315 less than 360 is 0
More than 45 degrees less than 90 to 90 greater than 90 less than 135 to 90
Other similar
In this way, no two points will be attributed to four vectors.
If the touch point is always 0 and 180 degrees, it is to slide the action left and right and assign a value of 1.
If the touch point is always 90 or 270 degrees, it is to slide up and down. action is assigned 2 values.
Follow the action to perform the following operations:

Multi-Point Similarity

Data structure?
Create a total Map storage school Id corresponding class Map
Allid stores the List of all class IDS
Class Map stores the class ID corresponding to the class Map
AllId stores the List of all class IDS
Student Map storage student ID corresponds to student picture linked list
AllId stores all student ID lists

Slide left and right to retrieve different images from the linked list
Slide up and down to retrieve the student Id in the class map

Android ListView blocks left and right sliding

This effect... we recommend that you understand SlidingMenu,

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.