滚动行为:ViewPager 中的 Horizo​​ntalScrollView

Scrolling behaviour: HorizontalScrollView inside ViewPager

我有一个包含水平滚动视图 (HSV) 的视图寻呼机 (VP)。如果 HSV 到达其边缘之一或根本无法滚动,则在被阻止的方向上进行新的滑动时,VP 应接管滚动到下一页。我犹豫要不要问这个问题,因为我发现了类似的问题:

Can I use Horizontal Scrollview Inside a Viewpager in Android?

horizontalscrollview inside viewpager

但是这个解决方案对我不起作用。 'v instanceof HorizontalScrollView' 为真但 viewPager 不滚动

关于如何实现所需行为的任​​何其他想法?

public class MyViewPager extends ViewPager {

public MyViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}
// Update 1
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return true;
    //return super.onInterceptTouchEvent(ev);
}

/**
 * 
 */
@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
    if (v instanceof HorizontalScrollView) {
        return true;
    }
    return super.canScroll(v, checkV, dx, x, y);
}

}

子视图:view_pager_page.xml:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
            <HorizontalScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center">

                    <include layout="@layout/" />
                </LinearLayout>
            </HorizontalScrollView>
        </LinearLayout>
    </FrameLayout>
</RelativeLayout>

父视图:view_pager.xml

<android.support.design.widget.CoordinatorLayout>
...
<LinearLayout>
<packagepath.MyViewPager
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
</packagepath.MyViewPager>
</LinearLayout>
...
<android.support.design.widget.CoordinatorLayout>

更新 1:覆盖 'onInterceptTouchEvent' 并让它始终 return 真正的 VP 滚动,但 HSV 不会。我认为这必须 return 只有当 HSV 到达边缘时才正确?如果是这种情况,我如何在这种方法中弄清楚?

更新二:重构了android的触摸事件机制,希望对如何拦截动作事件流有所了解。例如。在 HSV 中,我可以简单地 return false 让 VP 消耗这个和所有后续的运动事件。不幸的是,我需要两个 MotionEvent.MOVE 类型的运动事件来决定 HSV 或 VP 在到达边缘时是否应该滚动(如果 HSV 到达右边缘,向右滑动将 HSV 向后滚动,向左滑动滚动到 VP 的下一页) .但是,如果我跳过 MotionEvent.DOWN 操作,HSV 或 VP 都不会开始滚动……很难解决。有什么想法吗?

Touchevent Mechanism in Android

(警告:图不全,有错误,请大家指正:-))

更新 3:终于我让它工作了。了解Touchevent机制帮助很大,也是ZeroOne的第一条评论。当我有时间时,我会 post 我的解决方案。

1.ExtendViewPagerClass:

public class ViewPagerContainingHorizontalScrollView extends ViewPager {
private Float x_old;
private boolean bDoIntercept = false;
private boolean bHsvRightEdge = false;
private boolean bHsvLeftEdge = true;


public ViewPagerContainingHorizontalScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

private float calculateDistanceSwipe(MotionEvent ev){
    float distance = 0;
    if (x_old == null) {
        x_old = ev.getX();
    } else {
        distance = ev.getX() - x_old;
        x_old = null;
    }
    return distance;
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    mDoIntercept = false;
    if(ev.getAction() == MotionEvent.ACTION_MOVE) {
        float distance = calculateDistanceSwipe(ev);

        if (distance < 0) {//scrolling left direction
            if (bHsvRightEdge) { //HSV right edge
                bDoIntercept = true;
                //When scrolling slow VP may not switch page.
                //Then HSV snaps back into old position.
                //To allow HSV to scroll into non blocked direction set following to false.
                bHsvRightEdge = false;
            }
            bHsvLeftEdge = false;//scrolling left means left edge not reached
        } else if (distance > 0) {//scrolling right direction
            if (bHsvLeftEdge) { //HSV left edge
                bDoIntercept = true;
                //When scrolling slow VP may not switch page.
                //Then HSV snaps back into old position.
                //To allow HSV to scroll into non blocked direction set following to false.
                bHsvLeftEdge = false;
            }
            bHsvRightEdge = false;//scrolling right means right edge not reached
        }
    }
    return super.dispatchTouchEvent(ev);
}


@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if(bDoIntercept){
        return true;
    }else{
        return super.onInterceptTouchEvent(ev);
    }
}

@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
    if (v instanceof HorizontalScrollView) {
        HorizontalScrollView hsv = (HorizontalScrollView) v;
        int max_scrollX = hsv.getChildAt(0).getWidth() - hsv.getWidth();
        int min_scrollX = 0;
        int current_scroll_x = hsv.getScrollX();

        if (current_scroll_x == max_scrollX) { //HSV right edge
            bHsvRightEdge = true;
        }

        if (current_scroll_x == min_scrollX) { //HSV left edge
            bHsvLeftEdge = true;
        }
        return true;
    }
    return super.canScroll(v, checkV, dx, x, y);
}
}
  1. 在 XML 中使用此自定义 VP。
  2. 享受 VP 中的嵌套 HSV 滚动:-)

Touch Event Mechanism Overview for this specific case

我用自定义的 Horizo​​ntalScrollView 解决了这个问题。关键是覆盖 onTouchEvent() 方法和 return false 如果您在左边缘并向右滑动,或者在右边缘并向左滑动。返回 false 意味着此视图没有使用触摸事件,并且此事件可以冒泡备份视图层次结构以供 ViewPager 处理。

public class HorizontalScrollViewForViewPager extends HorizontalScrollView {

float old_x, old_y;

public HorizontalScrollViewForViewPager(Context context) {
    super(context);
}

public HorizontalScrollViewForViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public HorizontalScrollViewForViewPager(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    int action = ev.getActionMasked();

    if (action == MotionEvent.ACTION_DOWN) {
        //Start of touch.  Could be tap, could be drag.
        old_x = ev.getX();
        old_y = ev.getY();
    } else if (action == MotionEvent.ACTION_MOVE) {
        //Drag movement underway
        float deltaX = ev.getX() - old_x;
        float deltaY = ev.getY() - old_y;

        if (Math.abs(deltaX) > Math.abs(deltaY)) {
            //scrolling more left/right than up/down
            if (deltaX > 0 && getScrollX() == 0) {
                //dragging left, at left edge of HorizontalScrollView.  Don't handle this touch event, let it bubble up to ViewPager
                return false;
            } else {
                //dragging right.  Use first child to determine width of content inside HorizontalScrollView
                int childWidth = getChildAt(0).getWidth();
                if (deltaX < 0 && (this.getScrollX() + this.getWidth()) >= childWidth) {
                    //swiping left, and at right edge of HorizontalScrollView.  Don't handle this touch event, let it bubble up to ViewPager
                    return false;
                }
            }
        }
    }

    return super.onTouchEvent(ev);
}

}