Android:自动滚动 HorizontalScrollView
我使用以下代码来模拟选项卡,因为有更多宽度可以容纳的选项卡,用户可以向左或向右滚动以使选项卡按钮可见。这一切都有效,但是我还为用户提供了通过在选项卡内容上向左或向右滑动手指来在选项卡之间切换的功能。再说一遍——它有效。但是,当我跳到最右边的选项卡时,其相应的按钮几乎不可见。我想在 HorizontalScrollView
内自动滚动表格,以便所选选项卡按钮可见,但当我执行 HorizontalScrollView.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
HorizontalScrollView 没有任何问题,并且自动滚动工作正常。这只是我的代码中失败的一系列初始化。案件结案。
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.