我尝试在ViewPager中使用SlidingPaneLayout,如下所示
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scientific_graph_slidingPaneLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--
The first child view becomes the left pane.
-->
<ListView
android:id="@+id/left_pane"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left" />
<!--
The second child becomes the right (content) pane.
-->
<android.support.v4.view.ViewPager
android:id="@+id/scientific_graph_viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</android.support.v4.widget.SlidingPaneLayout>当我从左边拉的时候,SlidingPaneLayout会滑动;但是,当我从右边拉的时候,我似乎不能让ViewPager滑动。当我从右边拉的时候,它滑得很小,然后又弹了回来。
这样做有可能吗?有没有更好的方法来做这件事?
我发现,通过向上和向左移动手指,我可以滑动视图寻呼机。
发布于 2013-07-17 09:20:44
根本原因是#onInterceptTouchEvent的实现。SlidingPaneLayout的一个较早的实现调用了#canScroll,它将检查触摸目标是否可以滚动,如果可以,将滚动触摸目标,而不是滑动面板。最新的实现看起来总是在拖动阈值超过坡度时截取运动事件,除非X阻力超过坡度,Y阻力超过X阻力(如OP所示)。
一种解决方案是复制SlidingPaneLayout并进行一些更改以使其正常工作。这些更改包括:
if (adx > slop && ady > adx || canScroll(this,false,Math.round(x - mInitialMotionX),Math.round(x),Math.round(Y){ ... }
受保护的布尔型canScroll(View v,boolean checkV,int dx,int x,int y) { ... /*没有正确实现滚动接口的特殊情况ViewPagers */ checkV && (ViewCompat.canScrollHorizontally(v,-dx) || ((v CanScroll Dx) && canViewPagerScrollHorizontally((ViewPager) v,-dx))} boolean canViewPagerScrollHorizontally(ViewPager p,int dx) { return !(dx <0 && p.getCurrentItem() <= 0 || 0< dx && p.getAdapter().getCount() -1 <= p.getCurrentItem());}
可能有一种更优雅的方式来修复ViewDragHelper,但这是谷歌应该在未来的支持包更新中解决的问题。上面的技巧应该可以让布局与ViewPagers一起工作(以及其他水平滚动容器?)现在。
发布于 2015-01-16 05:57:41
在@Brien Colwell的解决方案的基础上,我编写了一个自定义的SlidingPaneLayout子类,它可以为您处理这个问题,并且还添加了边缘滑动功能,这样当用户向右滚动时,就不必为了打开窗格而一直滚动到左侧。
因为这是SlidingPaneLayout的一个子类,所以您不需要更改Java语言中的任何引用,只需确保实例化该类的一个实例(通常是在您的XML语言中)。
package com.ryanharter.android.view;
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;
/**
* SlidingPaneLayout that, if closed, checks if children can scroll before it intercepts
* touch events. This allows it to contain horizontally scrollable children without
* intercepting all of their touches.
*
* To handle cases where the user is scrolled very far to the right, but should still be
* able to open the pane without the need to scroll all the way back to the start, this
* view also adds edge touch detection, so it will intercept edge swipes to open the pane.
*/
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);
}
}https://stackoverflow.com/questions/17490899
复制相似问题