首页 > 系统 > Android > 正文

解决ViewPager和SlidingPaneLayout的滑动事件冲突问题

2019-10-22 18:17:33
字体:
来源:转载
供稿:网友

问题描述:

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的滑动事件冲突问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持VEVB武林网。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表