同步两个ScrollView视图的位置
我正在尝试同步两个 ScrollView 的位置。我正在尝试执行此操作以显示电视指南列表。
我创建了一个扩展RelativeLayout 的自定义类来显示指南。此相对布局有四个子项:左上角的 imageview、右上角显示列标题的 HorizontalScrollView、左下角显示行标题的 ScrollView 以及右下角包含列表的 ScrollView 。然后,此 ScrollView 包含一个 HorizontalScrollView,而 HorizontalScrollView 又包含一个带有多个显示数据的子视图的 LinearLayout。我希望这能清楚地解释它,但这里有一个图表可以使它更清楚:
____________
|__|___hsv___|
| | |
| | sv -> |
| | hsv -> |
|sv| ll -> |
| | etc |
| | |
|__|_________|
我这样设置是因为我希望指南列表水平和垂直滚动,但没有滚动视图可以做到这一点。另外,我希望无论指南列表位于什么位置,行标题和列标题都会显示,但我希望它们正确排列。所以我试图找到一种方法来同步两个 hsv 的位置,并同步两个 sv 的位置。我还尝试以一种方式来避免每隔几毫秒运行一个处理程序来轮询一个视图并在另一个视图上调用scrollTo。
我不确定这是否是最好的方法,但这就是我想到的。如果有人有任何其他建议,请随意!
I am trying to synchronize the positions of two ScrollViews. I'm trying to do this to display a tv guide listing.
I have created a custom class that extends RelativeLayout to display the guide. This relative layout has four children: an imageview in the top left corner, a HorizontalScrollView to display the column headers in the top right, a ScrollView to display the row headers at the bottom left, and a ScrollView in the bottom right that contains the listings. This ScrollView then contains a HorizontalScrollView, which in turn contains a LinearLayout with multiple child views that display the data. I hope this explains it clearly, but here's a diagram to make it clearer:
____________
|__|___hsv___|
| | |
| | sv -> |
| | hsv -> |
|sv| ll -> |
| | etc |
| | |
|__|_________|
I set it up like this because I wanted the guide listings to scroll both horizontally and vertically, but there's no scroll view that does this. Also, I want the row and column headers to display no matter what position the guide listings are at, but I want them to be lined up properly. So I'm trying to find a way to synchronize the positions of the two hsv's, and to also synchronize the positions of the two sv's. I'm also trying to do it in a way that avoids just running a handler every few milliseconds to poll one view and call scrollTo on the other.
I'm in no way sure that this is the best way to do it, but this is what I've come up with. If anybody has any other suggestions, please feel free!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
难道不值得实现 onTouchEvent (MotionEvent me) 在你的所有面板中?当滚动其中一个面板时,将调用此方法,它可以确保所有其他面板保持同步。
Wouldn't it be worth implementing onTouchEvent(MotionEvent me) in all your panels? When one of your panels is scrolled, this method would be called, and it can make sure that all the others are kept synchronized.
处理触摸事件并不能 100% 有效。不同视图中的滚动数量可能并不总是同步。
Handling the touch events does not work 100%. The amount of scrolls in different views may not be in sync always.
处理触摸事件是不够的,因为如果您快速滑动,则在释放触摸后滚动会继续一段时间。解决方案是重写computeScroll()方法。看看 https://github.com/chrisjenx/ParallaxScrollView 是如何完成的。
Handling the touch events is not enough, because if you do a fast flinge the scrolling goes on for a while after releasing the touch. The solution is to override the computeScroll() method. Have a look at https://github.com/chrisjenx/ParallaxScrollView how it is done there.
您可以看看这篇文章: 链接
这个人实际上在做什么,他创建了自定义 ScrollView 并覆盖
onScrollChanged
方法添加一个侦听器,然后在将它们绘制到屏幕之前对它们进行同步。您现在可以做的就是在
HorizontalScrollView
中添加一个ScrollView
作为主框架。左边的栏是一个HorizontalScrollView,顶部的栏是一个ScrollView。然后,将主ScrollView
和主HorizontalScrollView
注册到两个单独的ScrollManager
(请参阅上面的链接)。然后禁用 hsv 和 sv 的滚动并将它们添加到相应的 ScrollManagers 中。那么一切都应该工作正常。
-
PS。
如果您只使用侦听器本身而不使用
ScrollManager
,则同步延迟将为 1 帧。You may have a look at this post: Link
What this guy is actually doing he creates custom ScrollViews and overrides the method
onScrollChanged
, adds a listener and then synchronizes them before they are drawn to the screen.What you can do now is adding as main frame a
ScrollView
in aHorizontalScrollView
. The left bar would be aHorizontalScrollView
, and the top aScrollView
. Then you register the mainScrollView
and the mainHorizontalScrollView
to two seperateScrollManagers
(see link above).Then disable the scrolling of hsv and sv and add them to the appropriate
ScrollManagers
. Then everything should work fine.-
PS.
If you would use just the listeners themselves and not the
ScrollManager
, you would have a sync delay of 1 frame.我通过执行以下操作实现了相同的布局。
你会发现xml中的SyncedScrollView是自定义的scrollview,用于同步两个scrollview。
您需要参考下面两个 stackoverflow 答案。
检查下面的xml,
这里每个单元格都是TextView。因此您可以设置背景可绘制来绘制框(它看起来像表格)
您也可以动态添加左标题、顶部标题和正文 TextView 来动态创建表格。
这是布局的屏幕截图
抱歉我的英语纯正。
I have achieved same layout by doing below things.
You will find SyncedScrollView in xml is custom scrollview used to synchronize two scrollviews.
You need to refer below two stackoverflow answers.
check below xml
Each cell is TextView here. so you can set background drawable to draw boxes (It will look like table)
Also you can add left headers, top headers and body TextView dynamically to create table dynamically.
This is screen shot of layout
Sorry for my pure English.