TabWidget裹着Horizo​​ntalScrollView不与ViewPager滚动不与、裹着、TabWidget、Horizo

2023-09-06 03:54:07 作者:宿命太过致命

我在使用 TabHost 代替 ActionBarTabs ,并使其滚动能够我已经包裹着我 TabWidget Horizo​​ntalScrollView ,而 Horizo​​ntalScrollView 没有按'T本身滚动按照 ViewPager 。我在几个不同的方式使用scrollTo和fullScroll试过,但它不会改变任何东西。我需要什么做的就是这个工作正常?

 < TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@机器人:ID / tabhost
机器人:layout_width =match_parent
机器人:layout_height =match_parent>

<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直>

    < Horizo​​ntalScrollView
        机器人:ID =@ ID / horizo​​ntalScrollView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:fillViewport =真
        机器人:滚动条=@空>

        < TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=横向/>
    < / Horizo​​ntalScrollView>

    <的FrameLayout
        机器人:ID =@机器人:ID / tabcontent
        机器人:layout_width =0dp
        机器人:layout_height =0dp
        机器人:layout_weight =0/>

    < android.support.v4.view.ViewPager
        机器人:ID =@ ID / viewPager
        机器人:layout_width =match_parent
        机器人:layout_height =0dp
        机器人:layout_weight =1/>

< / LinearLayout中>
 

  @覆盖
    公共无效onTabChanged(字符串tabId){
        INT位置= mTab​​Host.getCurrentTab();
        mViewPager.setCurrentItem(位置);
        mHorizo​​ntalScrollView.scrollTo(位置,0);
    }

    @覆盖
    公共无效onPageScrolled(INT位置,浮positionOffset,
            INT positionOffsetPixels){
        mHorizo​​ntalScrollView.scrollTo(位置,0);
    }
 

解决方案

的水平滚动视图会当你调用修改 .refreshDrawableState()调用 .scrollTo(X,Y)方法。

另外一点需要注意的是, .scrollTo(X,Y)滚动,这样 X 定位在屏幕的左侧。你可能需要做一些数学与你的标签的坐标和横向滚动视图正确定位事情的宽度。你可以不叫 .scrollTo(位置,0)并让它工作,你希望(除非你的标签是1个像素宽)。

I'm having to use TabHost in place of ActionBarTabs and to make them scroll-able I've wrapped my TabWidget in a HorizontalScrollView, but the HorizontalScrollView doesn't scroll itself in accordance with ViewPager. I've tried using scrollTo and fullScroll in a couple of different ways, but it doesn't change anything. What do I need to do to get this working correctly?

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

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

    <HorizontalScrollView
        android:id="@id/horizontalScrollView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:scrollbars="@null" >

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

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

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

</LinearLayout>

        @Override
    public void onTabChanged(String tabId) {
        int position = mTabHost.getCurrentTab();
        mViewPager.setCurrentItem(position);
        mHorizontalScrollView.scrollTo(position, 0);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels) {
        mHorizontalScrollView.scrollTo(position, 0);
    }

解决方案

The horizontal scroll view will change when you call .refreshDrawableState() after calling the .scrollTo(x,y) method.

Another thing to watch out for is that .scrollTo(x,y) scrolls such that x is positioned on the left side of the screen. You may need to do some math with the coordinates of your tabs and the width of the horizontal scroll view to position things correctly. You can't call .scrollTo(position,0) and have it work the way you'd like (unless your tabs are 1 pixel wide).