Android:HorizontalScrollView 滚动问题
我有一个自定义的 HorizontalScrollView
、LinearLayout
作为子项,以及 LinearLayout
下的初始 9 个自定义视图。
当我向右滚动时,我需要向 LinearLayout 添加 3 个子视图,然后删除前三个子视图,这样一次只存在 9 个子视图。
我们根据视图 ID 添加了检测点,这样如果 currentViewId > > lastChildId - 0.33f * viewSizeLimit
其中 viewSizeLimit = 9
。如果这是有效的,那就是我们从 LinearLayout
添加/删除视图的时间。
我遇到的第一个问题是,当我从 LinearLayout 中删除视图时,子级会向左移动。因此,如果 currentViewId = 7
,并且当前视图位于检测区域内,那么我们将 3 个视图添加到末尾,并从前面删除 3 个视图。因此,由于转变,现在 currentViewId = 4
。
我们添加了 HorizontalScrollView
的 scrollBy
方法来补偿这种视图移动,并且在滚动速度不是那么快的情况下效果很好。
这是日志中的一个示例:
07-01 17:01:34.304: INFO/GTA(2476): currentViewId: 6
07-01 17:01:34.304: INFO/GTA(2476): Scroll Distance: 8
07-01 17:01:34.373: INFO/GTA(2476): currentViewId: 6
07-01 17:01:34.383: INFO/GTA(2476): Scroll Distance: 41
07-01 17:01:34.463: INFO/GTA(2476): currentViewId: 7
07-01 17:01:34.463: INFO/GTA(2476): Scroll Distance: 25
07-01 17:01:34.633: INFO/GTA(2476): Added new views to the end
07-01 17:01:34.643: INFO/GTA(2476): currentViewId: 4
07-01 17:01:34.653: INFO/GTA(2476): Scroll Distance: -1440
07-01 17:01:34.653: INFO/GTA(2476): Head View ID: 4 | Tail View ID: 12
07-01 17:01:34.673: INFO/GTA(2476): currentViewId: 7
07-01 17:01:34.673: INFO/GTA(2476): Scroll Distance: 22
07-01 17:01:34.733: INFO/GTA(2476): currentViewId: 7
这是快速滚动/滑动滚动视图时的日志:
07-01 17:03:48.633: INFO/GTA(2476): currentViewId: 6
07-01 17:03:48.643: INFO/GTA(2476): Scroll Distance: 158
07-01 17:03:48.693: INFO/GTA(2476): currentViewId: 6
07-01 17:03:48.704: INFO/GTA(2476): Scroll Distance: 124
07-01 17:03:48.753: INFO/GTA(2476): currentViewId: 7
07-01 17:03:48.753: INFO/GTA(2476): Scroll Distance: 114
07-01 17:03:48.914: INFO/GTA(2476): Added new views to the end
07-01 17:03:48.914: INFO/GTA(2476): currentViewId: 4
07-01 17:03:48.914: INFO/GTA(2476): Scroll Distance: -1440
07-01 17:03:48.914: INFO/GTA(2476): Head View ID: 4 | Tail View ID: 12
07-01 17:03:48.974: INFO/GTA(2476): currentViewId: 10
07-01 17:03:48.974: INFO/GTA(2476): Scroll Distance: 1843
07-01 17:03:49.194: INFO/GTA(2476): Added new views to the end
07-01 17:03:49.204: INFO/GTA(2476): currentViewId: 7
07-01 17:03:49.204: INFO/GTA(2476): Scroll Distance: -1440
07-01 17:03:49.204: INFO/GTA(2476): Head View ID: 7 | Tail View ID: 15
07-01 17:03:49.253: INFO/GTA(2476): currentViewId: 14
07-01 17:03:49.264: INFO/GTA(2476): Scroll Distance: 1866
07-01 17:03:49.403: INFO/GTA(2476): Added new views to the end
07-01 17:03:49.414: INFO/GTA(2476): currentViewId: 11
07-01 17:03:49.414: INFO/GTA(2476): Scroll Distance: -1440
07-01 17:03:49.414: INFO/GTA(2476): Head View ID: 10 | Tail View ID: 18
07-01 17:03:49.463: INFO/GTA(2476): currentViewId: 18
07-01 17:03:49.463: INFO/GTA(2476): Scroll Distance: 1551
07-01 17:03:49.703: INFO/GTA(2476): Added new views to the end
07-01 17:03:49.713: INFO/GTA(2476): currentViewId: 15
07-01 17:03:49.713: INFO/GTA(2476): Scroll Distance: -1440
07-01 17:03:49.713: INFO/GTA(2476): Head View ID: 13 | Tail View ID: 21
从日志中可以看出 HorizontalScrollView
自行滚动了绝对巨大的量(即 1843 年、1866 年、第1551章)我们看到的 -1440
值是我们添加的 scrollBy
补偿,用于设置视口中可见的正确视图。
所以现在的问题是,我似乎无法找出为什么 HorizontalScrollView
自身滚动这么大的距离。出现这种情况有什么原因吗?
除了使用 HorizontalScrollView 之外,还有其他更好的方法来实现这一点吗?
我应该考虑使用游戏引擎吗?
谢谢。
I have a customHorizontalScrollView
, LinearLayout
as a child, and an initial 9 custom views under the LinearLayout
.
When I scroll to the right, I need to add 3 more child views to the LinearLayout
, and then remove the first three children, such that only 9 child views are present at a time.
We added detection points based on view ids, in such a way that if the currentViewId > lastChildId - 0.33f * viewSizeLimit
where viewSizeLimit = 9
. If this is valid, that's the time we add/remove views from the LinearLayout
.
First issue I encountered is that when I remove views from the LinearLayout
, the children are shifted to the left. So if the currentViewId = 7
, and the current view is on the detection zone so we add 3 views to the end, and remove 3 views from the front. So now the currentViewId = 4
due to the shift.
We added scrollBy
method of the HorizontalScrollView
to compensate this view shift and works well if scrolling is not that fast.
Here's an example from the logs:
07-01 17:01:34.304: INFO/GTA(2476): currentViewId: 6
07-01 17:01:34.304: INFO/GTA(2476): Scroll Distance: 8
07-01 17:01:34.373: INFO/GTA(2476): currentViewId: 6
07-01 17:01:34.383: INFO/GTA(2476): Scroll Distance: 41
07-01 17:01:34.463: INFO/GTA(2476): currentViewId: 7
07-01 17:01:34.463: INFO/GTA(2476): Scroll Distance: 25
07-01 17:01:34.633: INFO/GTA(2476): Added new views to the end
07-01 17:01:34.643: INFO/GTA(2476): currentViewId: 4
07-01 17:01:34.653: INFO/GTA(2476): Scroll Distance: -1440
07-01 17:01:34.653: INFO/GTA(2476): Head View ID: 4 | Tail View ID: 12
07-01 17:01:34.673: INFO/GTA(2476): currentViewId: 7
07-01 17:01:34.673: INFO/GTA(2476): Scroll Distance: 22
07-01 17:01:34.733: INFO/GTA(2476): currentViewId: 7
Here's the log when scrolling/flinging the scrollview fast:
07-01 17:03:48.633: INFO/GTA(2476): currentViewId: 6
07-01 17:03:48.643: INFO/GTA(2476): Scroll Distance: 158
07-01 17:03:48.693: INFO/GTA(2476): currentViewId: 6
07-01 17:03:48.704: INFO/GTA(2476): Scroll Distance: 124
07-01 17:03:48.753: INFO/GTA(2476): currentViewId: 7
07-01 17:03:48.753: INFO/GTA(2476): Scroll Distance: 114
07-01 17:03:48.914: INFO/GTA(2476): Added new views to the end
07-01 17:03:48.914: INFO/GTA(2476): currentViewId: 4
07-01 17:03:48.914: INFO/GTA(2476): Scroll Distance: -1440
07-01 17:03:48.914: INFO/GTA(2476): Head View ID: 4 | Tail View ID: 12
07-01 17:03:48.974: INFO/GTA(2476): currentViewId: 10
07-01 17:03:48.974: INFO/GTA(2476): Scroll Distance: 1843
07-01 17:03:49.194: INFO/GTA(2476): Added new views to the end
07-01 17:03:49.204: INFO/GTA(2476): currentViewId: 7
07-01 17:03:49.204: INFO/GTA(2476): Scroll Distance: -1440
07-01 17:03:49.204: INFO/GTA(2476): Head View ID: 7 | Tail View ID: 15
07-01 17:03:49.253: INFO/GTA(2476): currentViewId: 14
07-01 17:03:49.264: INFO/GTA(2476): Scroll Distance: 1866
07-01 17:03:49.403: INFO/GTA(2476): Added new views to the end
07-01 17:03:49.414: INFO/GTA(2476): currentViewId: 11
07-01 17:03:49.414: INFO/GTA(2476): Scroll Distance: -1440
07-01 17:03:49.414: INFO/GTA(2476): Head View ID: 10 | Tail View ID: 18
07-01 17:03:49.463: INFO/GTA(2476): currentViewId: 18
07-01 17:03:49.463: INFO/GTA(2476): Scroll Distance: 1551
07-01 17:03:49.703: INFO/GTA(2476): Added new views to the end
07-01 17:03:49.713: INFO/GTA(2476): currentViewId: 15
07-01 17:03:49.713: INFO/GTA(2476): Scroll Distance: -1440
07-01 17:03:49.713: INFO/GTA(2476): Head View ID: 13 | Tail View ID: 21
From the logs, it can be seen that the HorizontalScrollView
scrolls by itself by definitely huge amounts (ie. 1843, 1866, 1551). The -1440
value we're seeing is the scrollBy
compensation we added to set the proper view visible in the viewport.
So problem now is that I can't seem to find out why the HorizontalScrollView
scrolls itself by such large distances. Any reason for this occurrence?
Is there any better way of implementing this aside from using a HorizontalScrollView
?
Should I consider using a game engine for this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想建议使用 Gallery,它基本上是一个水平 ListView。
这个想法是 SpinnerAdapter 实现为图库提供了宽度为区域 1/9 的视图,但在其他方面是标准实现。这会给你无限的滚动,我认为(假设我已经理解你的要求)会给你你正在寻找的效果。它还会有一个令人高兴的副作用,即卡入到位。
我希望这有帮助。
I'd like to suggest using a Gallery, which is basically a horizontal ListView.
The idea is that a SpinnerAdapter implementation provides the gallery with a view with a width of 1/9th of the area but is otherwise a standard implementation. This would give you unlimited scrolling that I think (assuming I've understood your requirements) would give you the effect you're looking for. It would also have the happy side-effect of snapping into position.
I hope that's helpful.