horizontalscrollview inside viewpager

冷暖自知 提交于 2019-12-06 09:55:09

In the end I made a custom pager and overrided the method canScroll like so

@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
    if (v instanceof HorizontalScrollView) {
        HorizontalScrollView scroll = (HorizontalScrollView) v;

        int vScrollX = scroll.getScrollX();
        TableLayout table = (TableLayout) scroll.getChildAt(scroll
                .getChildCount() - 1);
        int diff = (table.getRight() - (scroll.getWidth()
                + scroll.getScrollX() + table.getLeft()));

        if (vScrollX == 0 && diff <= 0) {// table without scroll
            if (dx > 20 && this.getCurrentItem() > 0) {
                this.setCurrentItem(this.getCurrentItem() - 1, true);
            } else if (dx < -20
                    && this.getCurrentItem() + 1 < this.getChildCount()) {
                this.setCurrentItem(this.getCurrentItem() + 1, true);
            }
            return false; // change page
        }
        if (vScrollX == 0 && dx > 20) {// left edge, swiping right
            if (this.getCurrentItem() > 0) {
                this.setCurrentItem(this.getCurrentItem() - 1, true);
            }
            return false; // change page
        }
        if (vScrollX == 0 && dx < -20) {// left edge, swiping left
            return true;// scroll
        }
        if (diff <= 0 && dx > 20) {// right edge, swiping right
            return true;// scroll
        }
        if (diff <= 0 && dx < -20) {// right edge, swiping left
            if (this.getCurrentItem() + 1 < this.getChildCount()) {
                this.setCurrentItem(this.getCurrentItem() + 1, true);
            }
            return false;// change page
        }
    }
    return super.canScroll(v, checkV, dx, x, y);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!