0

I am putting a Custom WebView inside of a Custom ViewPager: https://github.com/JakeWharton/Android-DirectionalViewPager

I've set the ViewPager to page in the vertical direction, which is the same direction my WebView scrolls, how ever the ViewPager intercepts all touch events.

So how it should work is that the WebView scrolls till it reaches the end, then once it is at the end of its scroll, the ViewPager should be allowed to page to the next page.

I guess, in the ViewPager, I need to find out, when a touch event occurs, the list of child Views that could potentially respond to the event, see if they are scrollable and respond appropriately.

How can I find out this list of potential child views that would receive the touch event if the ViewPager ignored the touch event?

SupaGu
  • 579
  • 6
  • 18

1 Answers1

-1

Take a cue from how Android does their 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);
}

It literally goes through the entire view hierarchy and does a hit test to see if the touch lies within the view bounds, and if so, check if that view can scroll.

David Liu
  • 9,426
  • 5
  • 40
  • 63