解决ViewPager和SlidingPaneLayout的滑动事件冲突问题
作者:Homilier
下面小编就为大家分享一篇解决ViewPager和SlidingPaneLayout的滑动事件冲突问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
问题描述:
ViewPager和SlidingPaneLayout的滑动事件冲突。
问题分析:
在手指左右滑动时,SlidingPaneLayout会屏蔽ViewPager的滑动事件。
解决办法:
自定义SlidingPaneLayout类
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; public class PagerEnabledSlidingPaneLayout extends SlidingPaneLayout { private float mInitialMotionX; private float mInitialMotionY; private float mEdgeSlop; public PagerEnabledSlidingPaneLayout(Context context) { this(context, null); } public PagerEnabledSlidingPaneLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public PagerEnabledSlidingPaneLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); ViewConfiguration config = ViewConfiguration.get(context); mEdgeSlop = config.getScaledEdgeSlop(); } @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(); if (mInitialMotionX > mEdgeSlop && !isOpen() && canScroll(this, false, Math.round(x - mInitialMotionX), Math.round(x), Math.round(y))) { MotionEvent cancelEvent = MotionEvent.obtain(ev); cancelEvent.setAction(MotionEvent.ACTION_CANCEL); return super.onInterceptTouchEvent(cancelEvent); } } } return super.onInterceptTouchEvent(ev); } }
以上这篇解决ViewPager和SlidingPaneLayout的滑动事件冲突问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。