Android handles slide conflicts with click events. android slide

Source: Internet
Author: User

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();    }}

 

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.