【问题标题】:Looped HorizontalScrollView循环的 HorizontalScrollView
【发布时间】:2012-09-04 12:52:50
【问题描述】:
我想在 HorizontallScrollView 中循环视图。
例子:
HorizontalScrollView holding 5 Views
When swiping through the Views the behaivior should be like this:
1 -> 2 -> 3 -> 4 -> 5 -> 1 -> 2 . . .
Or backwards:
1 <- 5 <- 4 <- 3 . . .
我正在寻找一个完成它的好方法!
【问题讨论】:
标签:
android
view
android-linearlayout
android-view
horizontalscrollview
【解决方案1】:
HorizontalScrollView 不太可能实现这一点。一个正确编写的PagerAdapter——它为getCount() 返回一个非常高的值并返回相同的N 个视图——也许可以用ViewPager 来解决这个问题。我不会使用任何基于片段的内置 PagerAdapter 实现,因为它们可能会对适配器的行为做出一些假设,而您的视图回收将违反这些假设。
【解决方案2】:
好吧,即使问题很老,它也值得回答:)
简单获取scrollViews的宽度或高度,获取x/y的滚动量。
HorizontalScrollView scroller;
int scrollerWidth = scroller.getMeasuredWidth();
int scrollX = scroller.getScrollX();
int activeViewCount = 0;
activeViewCount = (activeViewCount > 0) ? activeViewCount - 1 : 0;
scroller.smoothScrollTo(activeViewCount * scrollerWidth, 0);
if (scrollX >= scrollerWidth) {
scroller.scrollTo(0, 0);
}
这将形成一个连续循环。