Android:Horizo​​ntalScrollView 滚动问题

发布于 2024-11-18 01:15:07 字数 3500 浏览 4 评论 0原文

我有一个自定义的 Horizo​​ntalScrollViewLinearLayout 作为子项,以及 LinearLayout 下的初始 9 个自定义视图。

当我向右滚动时,我需要向 LinearLayout 添加 3 个子视图,然后删除前三个子视图,这样一次只存在 9 个子视图。

我们根据视图 ID 添加了检测点,这样如果 currentViewId > > lastChildId - 0.33f * viewSizeLimit 其中 viewSizeLimit = 9。如果这是有效的,那就是我们从 LinearLayout 添加/删除视图的时间。

我遇到的第一个问题是,当我从 LinearLayout 中删除视图时,子级会向左移动。因此,如果 currentViewId = 7,并且当前视图位于检测区域内,那么我们将 3 个视图添加到末尾,并从前面删除 3 个视图。因此,由于转变,现在 currentViewId = 4

我们添加了 Horizo​​ntalScrollViewscrollBy 方法来补偿这种视图移动,并且在滚动速度不是那么快的情况下效果很好。

这是日志中的一个示例:

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

从日志中可以看出 Horizo​​ntalScrollView 自行滚动了绝对巨大的量(即 1843 年、1866 年、第1551章)我们看到的 -1440 值是我们添加的 scrollBy 补偿,用于设置视口中可见的正确视图。

所以现在的问题是,我似乎无法找出为什么 Horizo​​ntalScrollView 自身滚动这么大的距离。出现这种情况有什么原因吗?

除了使用 Horizo​​ntalScrollView 之外,还有其他更好的方法来实现这一点吗?

我应该考虑使用游戏引擎吗?

谢谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫烠⑼条掵仅有一顆心 2024-11-25 01:15:07

我想建议使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文