【问题标题】:Android: Scrollable inside ViewPagerAndroid:可在 ViewPager 内滚动
【发布时间】:2013-01-12 02:10:56
【问题描述】:

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

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

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

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

如果 ViewPager 忽略了触摸事件,我如何才能找到可能接收到触摸事件的子视图列表?

【问题讨论】:

    标签: android webview touch android-viewpager scrollable


    【解决方案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);
    }
    

    它实际上会遍历整个视图层次结构并进行命中测试以查看触摸是否位于视图边界内,如果是,则检查该视图是否可以滚动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多