Android-gesturedetector optimizes the sliding effect between the left and right sides of the interface

Source: Internet
Author: User

First:

For my convenience, I marked the display area, for example:

The left and right arrows in indicate the direction and position of the user gesture. When we make a gesture at the arrow position of view1, we can achieve the switching effect of the left and right sliding interface, however, it cannot be recognized when the arrow position of view2 is used for gestures. If there are other controls in view1, such as buttons, this problem also occurs. Why? How can this problem be solved?

In fact, the reason is also very simple. You can also guess that the textview control in view2 processes the touch message, or the textview control does not respond to the left and right sliding gesture.

Relevant information andArticleLet's talk about how to solve the above problems. Here we will share with you my solutions.

To achieve the switching effect on the left-right sliding interface, we need to add the gesturedetector to detect the gesture as follows:Code:

Method 1:

 
Gesturedetector gesture_detector = NULL; gesture_detector = new gesturedetector (this, new mygesturelistener ());

Or use the following code:

Method 2:

 
Gesture_detector = new gesturedetector (this );

If you prefer method 1, method 1 must implement the gesture listening function of mygesturelistener (). The Code is as follows:

Class mygesturelistener implements view. ontouchlistener, ongesturelistener {// @ override public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {// you can determine the left and right gestures here, or add your own gesture judgment. // At the same time, add the corresponding gesture processing function here to complete the interface switching effect if (e1.getx ()-e2.getx ()> 100 & math. ABS (velocityx)> 50) {// animshownextpage ();} else if (e2.getx ()-e1.getx ()> 100 & math. ABS (velocityx)> 50) {// animshowprepage ();} return false;} // @ override public Boolean ondown (motionevent e) {return false ;} // @ override public void onlongpress (motionevent e) {}// @ override public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey) {return false ;} public void onshowpress (motionevent e) {} public Boolean onsingletapup (motionevent e) {return false;} public Boolean ontouch (view V, motionevent event) {// The first parameter V is the view clicked by the user. // in this example, it is the textview in view2. // At the same time, you can also use this parameter to determine which view if (event. getaction () = motionevent. action_down) {// if necessary, you can add relevant code for processing} else if (event. getaction () = motionevent. action_up) {} gesture_detector.ontouchevent (event); Return false ;}}

There are related comments in the code. I will not mention them here. It should be noted that the ontouch event is added to the class of the gesture listening, this is because the view is added to implements of the class. ontouchlistener is added to allow textview to use this listener function, and other views can also use this listener function.

Then, add the following code to the activity:

 
@ Override public Boolean ontouchevent (motionevent me) {return gesture_detector.ontouchevent (me );}

At this point, we can achieve switching between the left and right sliding interfaces when making a gesture at the arrow position of view1.

In addition, you also need to add an event listening function for textview in view2 in the oncreate function. The Code is as follows:

 
Textview txtview = (textview) findviewbyid (R. Id. content_view); txtview. settext (getshowtext (); txtview. setontouchlistener (New mygesturelistener ());

If there are other views in view1, use the above method to add the event listening function for this view.

If you create a gesturedetector using method 2 above, you can use the following code to set the view listener function:

Textview txtview = (textview) findviewbyid (R. Id. content_view); txtview. setontouchlistener (this );

At this point, the problem has been solved.

Summary: The cause of this problem is that there is no function for adding a gesture listener for textview. The solution is to add a gesture listener function for each view in the view1 area.

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.