Solve the conflict between rolling events of horizontalscrollview and touch of Components in Android-Revised Version

Source: Internet
Author: User
 

In the previous chapter, solving the conflict between the rolling event of horizontalscrollview and the touch of the component in Android, although the component can detect the up event while sliding, it cannot slide, here we can use another method to implement it, that is, when we touch, there is a situation in motionevent: motionevent. action_cancel. This event refers to the cancellation notification of the touch event. Although we cannot capture the up event, we can use motionevent. action_cancel to determine whether the user has previously performed the motionevent. action_down event, if the user motionevent. set flag when action_down
= 0. When cancel is entered, determine whether the current flag is equal to 0. If it is equal to 0, the up function is implemented here.

 

Of course, this method is not perfect because the user wants to implement the touch event of the component or the rolling event, because if the user wants to slide, however, the user can touch the touch event component. If the user wants to implement the touch event of the component but moves the component, this demarcation cannot be solved,

Another way to fold is the coordinate of the touch point. If the component is larger than the height, we need to use event. getx (), when the motionevent of the hand. action_move touch point and motionevent. when the interval between action_down and diff is greater than that set by the user, we think it is sliding. In this case, we set flag =-1. Otherwise, we think it is the touch event of the component, and we will rewrite onintercepttouchevent, the content is as follows:

@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {Log.e("tet", MainActivity.flag+"  ");if (MainActivity.flag == -1) {return super.onInterceptTouchEvent(ev);}else{return false;}}

Returns false to allow the component to implement the up event of touch.

 

Of course, we cannot determine without limit to the detection distance, because sometimes we need to change the background of the component when we are down, and change the background of the component again when we release the hand, we also need to add a time detection function. When it is within timeout (set by the user) and smaller than diff, we think it is a touch event of the component, otherwise it is considered a rolling event. To sum up, we can solve the conflict between rolling and touch.

Horizon. Java content:

package com.example.test;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.widget.HorizontalScrollView;public class Horizon extends HorizontalScrollView{public Horizon(Context context) {super(context);}public Horizon(Context context, AttributeSet attrs) {super(context, attrs);}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {Log.e("tet", MainActivity.flag+"  ");if (MainActivity.flag == 0) {return false;}else{return super.onInterceptTouchEvent(ev);}}}

Main class:

Package COM. example. test; import android. OS. bundle; import android. app. activity; import android. util. log; import android. view. motionevent; import android. view. view; import android. view. view. onclicklistener; import android. view. view. ontouchlistener; import android. widget. button; public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); btn1 = (button) findviewbyid (R. id. btn1); btn1.setontouchlistener (New ontouchlistener () {@ overridepublic Boolean ontouch (view V, motionevent event) {Switch (event. getaction () {Case motionevent. action_down: Flag = 0; down_t = system. currenttimemillis (); down_x = event. getx (); log. E ("test", "down" + event. getx (); break; Case motionevent. action_move: If (math. ABS (event. getx ()-down_x)> diff & (system. currenttimemillis ()-down_t) <= timeout) {flag = 1; // think it is a slide} log. E ("test", "move" + event. getx (); break; Case motionevent. action_up: log. E ("test", "up"); flag =-1; break; Case motionevent. action_cancel: If (flag = 0 | flag = 1) {flag =-1;} log. E ("test", "cancel"); break;} return false ;}}); btn3 = (button) findviewbyid (R. id. btn3); btn3.setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {log. E ("tec", "CLIC") ;}}) ;}public button btn1; Public button btn3; public static int flag =-1; public float down_x = 0; public float diff = 2; public long down_t = 0; public long timeout = 100 ;}

So we changed the image function in the down event above the touch and implemented it in the move else.
Case motionevent. action_move:
If (math. Abs (event. getx ()-down_x)> diff & (system. currenttimemillis ()-down_t) <= timeout ){
Flag = 1;
} Else {

}

Of course, it also needs to be fine-tuned that there are too many move events in the touch event. We can add a flag to determine the spacing and timeout only when the flag is = 0, similarly, only when the flag is set to 0, the flag must be set to 2, so that even if the move event does not enter the two judgment conditions, however, a condition must be added for the corresponding judgment conditions in the horizon class,

If (mainactivity. Flag = 0 | mainactivity. Flag = 2 ){
Return false;
} Else {
Return super. onintercepttouchevent (EV );
}

 

Similarly, the following move code is as follows:

Case motionevent. action_move: If (flag = 0 & math. ABS (event. getx ()-down_x)> diff & (system. currenttimemillis ()-down_t) <timeout) {flag = 1; // think it is sliding} else if (flag = 0 & math. ABS (event. getx ()-down_x) <= diff & (system. currenttimemillis ()-down_t)> = timeout) {flag = 2;} log. E ("test", "move" + event. getx (); break;

 

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.