我将 SlidingPanelLayout 与 ViewPager 一起使用,并且希望在用户将 ViewPager 片段作为内容片段时禁用用于打开菜单的滑动手势。我该怎么做?
问问题
766 次
1 回答
0
我里面有slidingPanel SlidingPaneLayout 和寻呼机ViewPager 我将展示我的例子。Firstable 创建自定义 SmartSlidingPane 类,如下例所示
public class SmartSlidingPane extends SlidingPaneLayout {
private float mInitialMotionX;
private float mInitialMotionY;
private float mEdgeSlop;
public SmartSlidingPane(Context context) {
this(context, null);
}
public SmartSlidingPane(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SmartSlidingPane(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);
}
}
那么你应该在你的xml中使用这个自定义类,比如
<app.info.rashjz.amuse.adapter.SmartSlidingPane
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/slidingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/menu" />
<LinearLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:orientation="vertical">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
</android.support.v4.view.ViewPager>
</LinearLayout>
</app.info.rashjz.amuse.adapter.SmartSlidingPane>
在您的主要活动中定义您的 SmartSlidingPane 布局,例如 (SmartSlidingPane) findViewById(R.id.slidingPanel); 就这样
于 2015-09-28T11:19:46.697 回答