问题描述:
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的滑动事件冲突解决方法就是小编分享给大家的全部内容了,希望能给大家一个参考。