Android - ViewPager scroll view

I use ViewPagerin my application, which hosts a horizontal layout of images for the implementation of the Paginated gallery.

When I test only view, i.e. as the main activity, it works fine. However, when I include this as part of another layout, the scroll (or click) becomes weird. If it seems to reset to its original position about halfway through the swipe (really quick clicks are the only thing that I'm still working on).

What ideas might come up?

Just in case, this has any meaning, the view is bloated from XML and added to the PagerAdapterinherited class .

+5
source share
1 answer

, ViewPager . , ViewPager, -, , . onInterceptTouchEvent() , rect , . - :

/**
 * Override to not intercept touch events within our scroller if it exists.
 */
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if(scrollerId != 0) {
        View scroller = findViewById(scrollerId);
        if(scroller != null) {
            Rect rect = new Rect();
            scroller.getHitRect(rect);
            if(rect.contains((int)ev.getX(), (int)ev.getY())) {
                return false;
            }
        }
    }
    return super.onInterceptTouchEvent(ev);
}

scrollerId ( ).

, ScrollView ViewPager, . ScrollView, , ViewPager .

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            xDistance = yDistance = 0f;
            lastX = ev.getX();
            lastY = ev.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            final float curX = ev.getX();
            final float curY = ev.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;
            if (xDistance > yDistance)
                return false;
    }

    return super.onInterceptTouchEvent(ev);
}
+9
source

All Articles