Android: HorizontalScrollView Scrolling Issue
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开发者_C百科 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.
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.
精彩评论