Android handles slide conflicts with click events. android slide
Requirement: A ViewGroup has multiple controls, similar to the common Tab layout at the bottom. You can click to switch between these sub-la s or slide between the left and right sides.
Implementation: customizes the parent control and determines whether to click or slide in the onInterceptTouchEvent method. If it determines whether to click or slide, it is directly handed to the child to respond to the click event. If it is a slide
Intercept the event and pass it to the caller through callback.
Advantage: This control only handles the distribution of touch events, and is suitable for various sliding and click conflicts ....
Use: directly use the control as the parent layout, and use the setmSetOnSlideListener callback to process the corresponding sliding event.
import android.content.Context;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.ViewConfiguration;import android.widget.RelativeLayout;public class MyRadioRelativeLayout extends RelativeLayout { public MyRadioRelativeLayout(Context context) { this(context, null); } public MyRadioRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } public MyRadioRelativeLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } private void initView() { } private boolean mScrolling; private float touchDownX; @Override public boolean onInterceptTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: touchDownX = event.getX(); mScrolling = false; break; case MotionEvent.ACTION_MOVE: if (Math.abs(touchDownX - event.getX()) >= ViewConfiguration.get( getContext()).getScaledTouchSlop()) { mScrolling = true; } else { mScrolling = false; } break; case MotionEvent.ACTION_UP: mScrolling = false; break; } return mScrolling; } float x1 = 0; float x2 = 0; @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: return true; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: x2 = event.getX(); if (touchDownX - x2 > DensityUtil.dip2px(getContext(), 40)) { if(mSetOnSlideListener!=null){ mSetOnSlideListener.onRightToLeftSlide(); } } if (touchDownX - x2 < -DensityUtil.dip2px(getContext(), 40)) { if(mSetOnSlideListener!=null){ mSetOnSlideListener.onLeftToRightSlide(); } } break; } return super.onTouchEvent(event); } private setOnSlideListener mSetOnSlideListener; public setOnSlideListener getmSetOnSlideListener() { return mSetOnSlideListener; } public void setmSetOnSlideListener(setOnSlideListener mSetOnSlideListener) { this.mSetOnSlideListener = mSetOnSlideListener; } public interface setOnSlideListener{ void onRightToLeftSlide(); void onLeftToRightSlide(); }}