使用ViewPager滑动时,TabHost选项卡不会水平滚动

Nap*_*ead 0 android horizontal-scrolling android-tabhost android-viewpager actionbarsherlock

我正在编写一个使用许多片段的应用程序.我使用了ActionBarSherlock(带ActionBar).为了解决bug#240,我决定切换一个功能正常的实现来使用ActionBarSherlock和TabHost.经过半个小时的工作,它工作正常,除了我在页面之间滑动:相应的选项卡被选中,但没有居中,往往甚至看不到,因此用户无法看到选择了哪个选项卡.

在此输入图像描述

在图片中,您可以看到可见片段("WEEKLY S .."),但标签只有一半可见.下一次滑动只会使"WEEKLY S ..."看起来像"CALENDAR EVENTS",除非我手动滑动TabWidget,否则我不知道选择了哪个Tab.

这是我的activity_main.xml:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <HorizontalScrollView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:scrollbars="none" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:tabStripEnabled="true"
                android:orientation="horizontal" />

        </HorizontalScrollView>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="0"/>

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>

    </LinearLayout>

</TabHost>
Run Code Online (Sandbox Code Playgroud)

而我的TabsAdapter与Jake WhartonFragmentTabsPager示例相同.

欢迎任何建议!

Nap*_*ead 6

感谢这篇文章,特别是jucas,我设法让它发挥作用.这是代码:

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    View tabView = mTabHost.getTabWidget().getChildAt(position);
    if (tabView != null)
    {
        final int width = mHorizontalScroll.getWidth();
        final int scrollPos = tabView.getLeft() - (width - tabView.getWidth()) / 2;
        mHorizontalScroll.scrollTo(scrollPos, 0);
    } else {
        mHorizontalScroll.scrollBy(positionOffsetPixels, 0);
    }
}
Run Code Online (Sandbox Code Playgroud)

当然,我必须mHorizontalScroll在几个地方进行初始化.(如果有人不确定如何操作,我会很乐意发布完整的代码.)