Android:自动滚动 Horizo​​ntalScrollView

发布于 2024-09-01 22:10:08 字数 2444 浏览 4 评论 0原文

我使用以下代码来模拟选项卡,因为有更多宽度可以容纳的选项卡,用户可以向左或向右滚动以使选项卡按钮可见。这一切都有效,但是我还为用户提供了通过在选项卡内容上向左或向右滑动手指来在选项卡之间切换的功能。再说一遍——它有效。但是,当我跳到最右边的选项卡时,其相应的按钮几乎不可见。我想在 Horizo​​ntalScrollView 内自动滚动表格,以便所选选项卡按钮可见,但当我执行 Horizo​​ntalScrollView.smoothScrollTo(300, 0) 时,什么也没有发生。无论我将第一个 x 参数设置得有多高,都不会移动(是的,我确实有一个计算精确位置的算法)。

以下是滚动选项卡按钮的 XML 代码

<HorizontalScrollView android:layout_width="fill_parent"
    android:background="@color/tabs_header" android:layout_height="55dip"
    android:scrollbars="none" android:id="@+id/tabsButtonView">
    <TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent" android:layout_weight="1"
            android:layout_height="0dip" android:paddingTop="5dip" android:paddingLeft="3dip">
            <ImageButton android:src="@drawable/linkup_logo_small" android:id="@+id/tabBtt0"
                android:layout_width="wrap_content" android:layout_marginLeft="2dip" android:layout_marginRight="2dip"
                android:layout_height="fill_parent" android:padding="5dip" android:background="@drawable/tab_selected"></ImageButton>
            <ImageButton android:src="@drawable/simplyhired_small" android:id="@+id/tabBtt1"
                android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_marginLeft="2dip"
                android:layout_marginRight="2dip" android:padding="5dip" android:background="@drawable/tab_normal"></ImageButton>
            <ImageButton android:src="@drawable/indeedcom_small" android:id="@+id/tabBtt2"
                android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dip"
                android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/tab_normal"></ImageButton>
            <ImageButton android:src="@drawable/careerbuilder_logo_small" android:id="@+id/tabBtt3"
                android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dip"
                android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/tab_normal"></ImageButton>
        </TableRow>
    </TableLayout>
</HorizontalScrollView>

I'm using the following code to simulate tabs and since there are more tabs that width can accommodate user can scroll left or right to make a tab button visible. It all works, however I also provide user with ability to fling between tabs by swiping finger left or right on the tab contents. Again - it works. But when I fling to the rightmost tab its corresponding button is barely visible. I want to autoscroll table inside the HorizontalScrollView so the selected tab button will be visible but when I execute HorizontalScrollView.smoothScrollTo(300, 0) nothing happens. It doen't matter how high I set first x parameter nothing will ever move (yes I do have an algorithm to calculate exact position).

Here's XML code for scrolling tab buttons

<HorizontalScrollView android:layout_width="fill_parent"
    android:background="@color/tabs_header" android:layout_height="55dip"
    android:scrollbars="none" android:id="@+id/tabsButtonView">
    <TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent" android:layout_weight="1"
            android:layout_height="0dip" android:paddingTop="5dip" android:paddingLeft="3dip">
            <ImageButton android:src="@drawable/linkup_logo_small" android:id="@+id/tabBtt0"
                android:layout_width="wrap_content" android:layout_marginLeft="2dip" android:layout_marginRight="2dip"
                android:layout_height="fill_parent" android:padding="5dip" android:background="@drawable/tab_selected"></ImageButton>
            <ImageButton android:src="@drawable/simplyhired_small" android:id="@+id/tabBtt1"
                android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_marginLeft="2dip"
                android:layout_marginRight="2dip" android:padding="5dip" android:background="@drawable/tab_normal"></ImageButton>
            <ImageButton android:src="@drawable/indeedcom_small" android:id="@+id/tabBtt2"
                android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dip"
                android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/tab_normal"></ImageButton>
            <ImageButton android:src="@drawable/careerbuilder_logo_small" android:id="@+id/tabBtt3"
                android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dip"
                android:layout_marginLeft="2dip" android:layout_marginRight="2dip" android:background="@drawable/tab_normal"></ImageButton>
        </TableRow>
    </TableLayout>
</HorizontalScrollView>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

戒ㄋ 2024-09-08 22:10:08

Horizo​​ntalScrollView 没有任何问题,并且自动滚动工作正常。这只是我的代码中失败的一系列初始化。案件结案。

There's nothing wrong with HorizontalScrollView and autoscrolling is working perfectly. It was simply a sequence of initialization that was failing in my code. Case closed.

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