【问题标题】:TabHost tabs are not horizontally scrolling when swiping using ViewPager使用 ViewPager 滑动时 TabHost 选项卡不会水平滚动
【发布时间】:2013-05-05 01:40:59
【问题描述】:

我正在编写一个使用许多片段的应用程序。我使用过 ActionBarSherlock(带有 ActionBar)。为了解决错误 #240,我决定切换一个功能正常的实现以使用带有 TabHost 的 ActionBarSherlock。 经过半个小时的工作,它工作正常,除了我在页面之间滑动时:选择了对应的选项卡,但不居中,通常甚至不可见所以用户不能查看选择了哪个选项卡。

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

这是我的 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>

而且我的 TabsAdapter 和Jake WhartonFragmentTabsPager 的例子一样。

欢迎提出任何建议!

【问题讨论】:

    标签: android actionbarsherlock android-viewpager android-tabhost horizontal-scrolling


    【解决方案1】:

    我在 onPageChangeListener 中添加了以下代码来解决问题。

    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
    
            @Override
            public void onPageSelected(int position) {
                // on changing the page make respected tab selected
                //actionBar.setSelectedNavigationItem(position);
                mTabHost.setCurrentTab(position);
            }
    
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
                View tabView = mTabHost.getTabWidget().getChildAt(position);
                View mHorizontalScroll = (HorizontalScrollView) findViewById(R.id.mHorizontalScroll);
                if (tabView != null)
                {
                    int width = mHorizontalScroll.getWidth();
                    int scrollPos = tabView.getLeft() - (width - tabView.getWidth()) / 2;
                    mHorizontalScroll.scrollTo(scrollPos, 0);
                } else {
                    mHorizontalScroll.scrollBy(positionOffsetPixels, 0);
                }
            }
    
            @Override
            public void onPageScrollStateChanged(int arg0) {
    
            }
        });
    

    【讨论】:

    • 他们询问了完整的代码,我发送了我的代码来解释代码。它工作正常。
    【解决方案2】:

    感谢post,尤其感谢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);
        }
    }
    

    当然,我必须在几个地方初始化mHorizontalScroll。 (如果有人不知道该怎么做,我很乐意发布完整的代码。)

    【讨论】:

    • 我猜他毕竟不是那么愿意分享:-0!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多