在项目中碰到一个问题,我使用的SlidingPaneLayout与主页面中的ViewPager 出现滑动冲突问题,
向右滑没有问题,但是向左滑会先打开SlidingPaneLayout侧滑栏,使viewPager失去焦点无法滑动,
我在网上找了很久,原生属性没有解决这一问题的,翻墙到overflow找到了一种方法:重写控件类,下面贴出重写代码,和Demo应用代码:
自定义PagerEnabledSlidingPaneLayout继承SlidingPaneLayout:
package com.example.admin.YingDiDemo.util; 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; /** * Created by admin on 2018/3/31. */ 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(); // 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); } }
在布局文件中使用:
<com.example.admin.Demo.util.PagerEnabledSlidingPaneLayout android:id="@+id/sliding" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/menu_layout" /> <include layout="@layout/content_layout" android:clickable="false" /> </com.example.admin.Demo.util.PagerEnabledSlidingPaneLayout>