同步两个ScrollView视图的位置

发布于 2024-08-26 09:23:31 字数 722 浏览 6 评论 0原文

我正在尝试同步两个 ScrollView 的位置。我正在尝试执行此操作以显示电视指南列表。

我创建了一个扩展RelativeLayout 的自定义类来显示指南。此相对布局有四个子项:左上角的 imageview、右上角显示列标题的 Horizo​​ntalScrollView、左下角显示行标题的 ScrollView 以及右下角包含列表的 ScrollView 。然后,此 ScrollView 包含一个 Horizo​​ntalScrollView,而 Horizo​​ntalScrollView 又包含一个带有多个显示数据的子视图的 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 技术交流群。

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

发布评论

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

评论(5

新人笑 2024-09-02 09:23:31

难道不值得实现 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.

迷你仙 2024-09-02 09:23:31

处理触摸事件并不能 100% 有效。不同视图中的滚动数量可能并不总是同步。

Handling the touch events does not work 100%. The amount of scrolls in different views may not be in sync always.

一紙繁鸢 2024-09-02 09:23:31

处理触摸事件是不够的,因为如果您快速滑动,则在释放触摸后滚动会继续一段时间。解决方案是重写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.

原来是傀儡 2024-09-02 09:23:31

您可以看看这篇文章: 链接

这个人实际上在做什么,他创建了自定义 ScrollView 并覆盖onScrollChanged 方法添加一个侦听器,然后在将它们绘制到屏幕之前对它们进行同步。

您现在可以做的就是在 Horizo​​ntalScrollView 中添加一个 ScrollView 作为主框架。左边的栏是一个Horizo​​ntalScrollView,顶部的栏是一个ScrollView。然后,将主 ScrollView 和主 Horizo​​ntalScrollView 注册到两个单独的 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 a HorizontalScrollView. The left bar would be a HorizontalScrollView, and the top a ScrollView. Then you register the main ScrollView and the main HorizontalScrollView to two seperate ScrollManagers (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.

蹲墙角沉默 2024-09-02 09:23:31

我通过执行以下操作实现了相同的布局。

你会发现xml中的SyncedScrollView是自定义的scrollview,用于同步两个scrollview。

您需要参考下面两个 stackoverflow 答案。

  1. SyncedScrollView 和ig答案
  2. 优化SyncedScrollView 我的答案

检查下面的xml,

<RelativeLayout
    android:id="@+id/activity_main_linear_before_all_scroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/activity_main_empty_box"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:text="xx"
        android:gravity="center" />

    <com.example.SyncedScrollView
        android:id="@+id/activity_main_observable_scrollview_1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/activity_main_empty_box"
        android:fadeScrollbars="false"
        android:overScrollMode="never"
        android:scrollbars="none">

        <LinearLayout
            android:id="@+id/activity_main_linear_left_headers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:gravity="center"
                android:padding="5dp"
                android:text="row 1" />

            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:gravity="center"
                android:padding="5dp"
                android:text="row 2" />

            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:gravity="center"
                android:padding="5dp"
                android:text="row 3" />

        </LinearLayout>

    </com.example.SyncedScrollView>

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toEndOf="@+id/activity_main_empty_box"
        android:layout_toRightOf="@+id/activity_main_empty_box"
        android:fadeScrollbars="false"
        android:overScrollMode="never">

        <RelativeLayout
            android:id="@+id/activity_main_body_relative"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/activity_main_top_headers"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="50dp"
                    android:layout_height="30dp"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="column 1" />

                <TextView
                    android:layout_width="50dp"
                    android:layout_height="30dp"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="column 2" />

                <TextView
                    android:layout_width="50dp"
                    android:layout_height="30dp"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="column 3" />

            </LinearLayout>

            <com.example.SyncedScrollView
                android:id="@+id/activity_main_observable_scrollview_2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/activity_main_top_headers"
                android:fadeScrollbars="false"
                android:overScrollMode="never"
                android:scrollbars="vertical">

                <LinearLayout
                    android:id="@+id/activity_main_body"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <LinearLayout
                        android:id="@+id/activity_main_body_row_1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="1,1" />

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="1,2" />

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="1,3" />

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/activity_main_body_row_2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="2,1" />

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="2,2" />

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="2,3" />

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/activity_main_body_row_3"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="3,1" />

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="3,2" />

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="3,3" />

                    </LinearLayout>

                </LinearLayout>

            </com.example.SyncedScrollView>

        </RelativeLayout>

    </HorizontalScrollView>

</RelativeLayout>

这里每个单元格都是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.

  1. SynchedScrollView andig answer
  2. optimized SynchedScrollView my answer

check below xml

<RelativeLayout
    android:id="@+id/activity_main_linear_before_all_scroll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/activity_main_empty_box"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:text="xx"
        android:gravity="center" />

    <com.example.SyncedScrollView
        android:id="@+id/activity_main_observable_scrollview_1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_below="@+id/activity_main_empty_box"
        android:fadeScrollbars="false"
        android:overScrollMode="never"
        android:scrollbars="none">

        <LinearLayout
            android:id="@+id/activity_main_linear_left_headers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:gravity="center"
                android:padding="5dp"
                android:text="row 1" />

            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:gravity="center"
                android:padding="5dp"
                android:text="row 2" />

            <TextView
                android:layout_width="50dp"
                android:layout_height="30dp"
                android:gravity="center"
                android:padding="5dp"
                android:text="row 3" />

        </LinearLayout>

    </com.example.SyncedScrollView>

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toEndOf="@+id/activity_main_empty_box"
        android:layout_toRightOf="@+id/activity_main_empty_box"
        android:fadeScrollbars="false"
        android:overScrollMode="never">

        <RelativeLayout
            android:id="@+id/activity_main_body_relative"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:id="@+id/activity_main_top_headers"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="50dp"
                    android:layout_height="30dp"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="column 1" />

                <TextView
                    android:layout_width="50dp"
                    android:layout_height="30dp"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="column 2" />

                <TextView
                    android:layout_width="50dp"
                    android:layout_height="30dp"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="column 3" />

            </LinearLayout>

            <com.example.SyncedScrollView
                android:id="@+id/activity_main_observable_scrollview_2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/activity_main_top_headers"
                android:fadeScrollbars="false"
                android:overScrollMode="never"
                android:scrollbars="vertical">

                <LinearLayout
                    android:id="@+id/activity_main_body"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <LinearLayout
                        android:id="@+id/activity_main_body_row_1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="1,1" />

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="1,2" />

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="1,3" />

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/activity_main_body_row_2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="2,1" />

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="2,2" />

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="2,3" />

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/activity_main_body_row_3"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="3,1" />

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="3,2" />

                        <TextView
                            android:layout_width="50dp"
                            android:layout_height="30dp"
                            android:gravity="center"
                            android:padding="5dp"
                            android:text="3,3" />

                    </LinearLayout>

                </LinearLayout>

            </com.example.SyncedScrollView>

        </RelativeLayout>

    </HorizontalScrollView>

</RelativeLayout>

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
This is screen shot of layout

Sorry for my pure English.

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