android - ScrollView中的TabHost,选中时滚动到选项卡

标签 android tabs scroll android-tabhost horizontalscrollview

我在 Horizo​​ntalScrollView 中有一个 TabHost,有时可以有超过 20 个选项卡。在每个选项卡 Activity 中,我可以按“下一步”按钮,指示 TabHost 移至下一个选项卡。问题是,如果 ScrollView 不在屏幕上,我无法让 ScrollView 滚动到选定的选项卡。

有人可以告诉我如何做到这一点吗?

最佳答案

您必须将 xml 中的 TabWidget“包装”到 Horizo​​ntalScrollView 中:

...

    <HorizontalScrollView android:layout_width="fill_parent"
                          android:layout_height="wrap_content"
                          android:fillViewport="true"
                          android:scrollbars="none"
                          android:id="@+id/tabsHorizontalScrollView">

        <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"/>
    </HorizontalScrollView>

...

并设置为TabHost TabHost.OnTabChangeListener

@Override
public void onTabChanged(final String tag) {
    View tabView = tabHost.getCurrentTabView();
    int scrollPos = tabView.getLeft() - (tabsHorizontalScrollView.getWidth() - tabView.getWidth()) / 2;
    tabsHorizontalScrollView.smoothScrollTo(scrollPos, 0);
}

关于android - ScrollView中的TabHost,选中时滚动到选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7875939/

相关文章:

android - 想要在移动操作系统中为 App 永久存储两个变量

android - 如何获得最大文本行

android - 将 PDFViewCtrl 滚动到文档中的绝对位置

html - 另一个站点的滚动效果

android - 如何在 android 中使用 statuses/update_with_media 将图像发布到 Twitter

android - 键盘启动时如何设置编辑文本?

jquery - 如何更改事件选项卡中图标字体的颜色?

javascript - 使用 Bootstrap nav-tabs 从另一个选项卡跳转到特定选项卡

google-chrome - 停止 Chrome Tab sleep /休眠 2020

flutter - 使用NotificationListener检测滚动到列表末尾