viewpager内的horizontalscrollview [英] horizontalscrollview inside viewpager
问题描述
我有一个2至4页的viewpager.这些页面之一以表格形式显示数据.为此,我使用垂直滚动和水平滚动.
I have a viewpager with 2 to 4 pages. One of these pages presentes data in table form. To do so i use a vertical scroll and a horizontal scroll.
由于horizontalscrollview和viewpager之间的冲突,有时滚动不起作用.
Due to conflicts between horizontalscrollview and viewpager, sometimes, scrolling does nothing.
I want to scroll the page while it has scroll left. When it reaches the edge, another swipe gesture will make it change the page of view pager, following guideline from http://developer.android.com/design/patterns/swipe-views.html where it states: "If a view contains content that exceeds the width of the screen such as a wide email message, make sure the user's initial swipes will scroll horizontally within the view. Once the end of the content is reached, an additional swipe should navigate to the next view. In addition, support the use of edge swipes to immediately navigate between views when content scrolls horizontally."
我该如何实现?
推荐答案
最后,我制作了一个自定义传呼器,并覆盖了canScroll这样的方法
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);
}
这篇关于viewpager内的horizontalscrollview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!