ViewPager和SlidingPaneLayout的滑动冲突事件

来源:互联网 发布:狸窝视频剪辑软件 编辑:程序博客网 时间:2024/05/16 08:51

我们在做需要带有侧滑栏的框架时难免遇到ViewPager与Fragment的联用,这时我们在页面右划时没有问题,但是当页面需要左划时,就会触发左侧隐藏的SlidingPaneLayout侧滑栏,所以我们下面自定义控件来继承SlidingPaneLayout,写上对应的处理方法来解决冲突事件的发生。

代码如下:

package com.example.dotawang.puding.custom;import android.content.Context;import android.support.v4.view.MotionEventCompat;import android.support.v4.widget.SlidingPaneLayout;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.ViewConfiguration;/*在使用侧滑菜单的时候如果主界面中有ViewPager的时候调用该自定义控件,避免二者的冲突事件的发生 */public class PageEnabledSlidingPaneLayout extends SlidingPaneLayout {    private float mInitialMotionX;    private float mInitialMotionY;    private float mEdgeSlop;//手滑动的距离    public PageEnabledSlidingPaneLayout(Context context) {        this(context, null);    }    public PageEnabledSlidingPaneLayout(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public PageEnabledSlidingPaneLayout(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        ViewConfiguration config = ViewConfiguration.get(context);        mEdgeSlop = config.getScaledEdgeSlop();//getScaledTouchSlop是一个距离    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        switch (MotionEventCompat.getActionMasked(ev)) {            case MotionEvent.ACTION_DOWN: {                mInitialMotionX = ev.getX();                mInitialMotionY = ev.getY();                break;            }            case MotionEvent.ACTION_MOVE: {                final float x = ev.getX();                final float y = ev.getY();                // The user should always be able to "close" the pane, so we only check                // for child scrollability if the pane is currently closed.                if (mInitialMotionX > mEdgeSlop && !isOpen() && canScroll(this, false,                        Math.round(x - mInitialMotionX), Math.round(x), Math.round(y))) {                    // How do we set super.mIsUnableToDrag = true?                    // send the parent a cancel event                    MotionEvent cancelEvent = MotionEvent.obtain(ev);                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL);                    return super.onInterceptTouchEvent(cancelEvent);                }            }        }        return super.onInterceptTouchEvent(ev);    }

最后,在布局中直接引用新建的类的名字做控件即可!


0 0