Android:在ViewPager中可滚动

时间:2013-01-12 02:10:57

标签: android webview touch android-viewpager scrollable

我将Custom WebView放在Custom ViewPager中: https://github.com/JakeWharton/Android-DirectionalViewPager

我已将ViewPager设置为垂直方向的页面,这与我的WebView滚动方向相同,但ViewPager如何拦截所有触摸事件。

所以它应该如何工作是WebView滚动直到它到达结尾,然后一旦滚动结束,ViewPager应该被允许页面到下一页。

我想,在ViewPager中,当发生触摸事件时,我需要找出可能对事件做出响应的子视图列表,看看它们是否可滚动并做出适当的响应。

如果ViewPager忽略触摸事件,如何找到可能会收到触摸事件的潜在子视图列表?

1 个答案:

答案 0 :(得分:-1)

从Android的ViewPager方式中获取提示。

/**
 * Tests scrollability within child views of v given a delta of dx.
 *
 * @param v View to test for horizontal scrollability
 * @param checkV Whether the view v passed should itself be checked for scrollability (true),
 *               or just its children (false).
 * @param dx Delta scrolled in pixels
 * @param x X coordinate of the active touch point
 * @param y Y coordinate of the active touch point
 * @return true if child views of v can be scrolled by delta of dx.
 */
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
    if (v instanceof ViewGroup) {
        final ViewGroup group = (ViewGroup) v;
        final int scrollX = v.getScrollX();
        final int scrollY = v.getScrollY();
        final int count = group.getChildCount();
        // Count backwards - let topmost views consume scroll distance first.
        for (int i = count - 1; i >= 0; i--) {
            // TODO: Add versioned support here for transformed views.
            // This will not work for transformed views in Honeycomb+
            final View child = group.getChildAt(i);
            if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight() &&
                    y + scrollY >= child.getTop() && y + scrollY < child.getBottom() &&
                    canScroll(child, true, dx, x + scrollX - child.getLeft(),
                            y + scrollY - child.getTop())) {
                return true;
            }
        }
    }

    return checkV && ViewCompat.canScrollHorizontally(v, -dx);
}

它完全通过整个视图层次结构并进行命中测试以查看触摸是否位于视图范围内,如果是,则检查该视图是否可以滚动。