2012-03-10 38 views
4
滚动

我有在地方使用TabHostActionBarTabs并使其滚动能够我包裹着我的TabWidgetHorizontalScrollView,但HorizontalScrollView不滚动本身根据ViewPager。我尝试过使用scrollTo和fullScroll几种不同的方式,但它不会改变任何东西。我需要做些什么才能正确地工作?TabWidget包裹在Horizo​​ntalScrollView不ViewPager

<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); 
    } 

回答

5

水平滚动视图会发生变化。

要注意的另一件事是.scrollTo(x,y)滚动,使得x位于屏幕的左侧。您可能需要使用制表符的坐标和水平滚动视图的宽度进行一些数学运算,以正确定位事物。您不能拨打.scrollTo(position,0),并按照您喜欢的方式工作(除非您的标签宽度为1像素)。

+1

我不知道'refreshDrawableState()'。自从我发布后,我已经了解了左边的'x'坐标。 'scrollBy()'似乎比'scrollTo()'更好。感谢您的提示。 – adneal 2012-03-13 00:29:44

-1

ViewPager对滚动行为的大脑。

我会从您的布局中删除Horizo​​ntalScrollView。其余的看起来很好。

然后我会检查这个谷歌提供的代码示例,比如你要完成的,我基于ViewPager接口:http://developer.android.com/resources/samples/Support4Demos/src/com/example/android/supportv4/app/FragmentTabsPager.html

你会发现,你上面定义的相应方法没有什么用滚动来做,除了设置要滚动的页面。 ViewPager在内部处理平滑滚动。当你调用.scrollTo(x,y)方法调用后.refreshDrawableState()

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

    @Override 
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
    } 
+0

我需要将我的'TabWidget'封装在'Horizo​​ntalScrollView'中,以便用户可以单独滚动选项卡。问题是'ViewPager'时'Horizo​​ntalScrollView'不会移动。 'ViewPager'和其他一切工作正常,我没有使用这些问题。 'FragmentTabsPager'的例子就是我最初提到的。 – adneal 2012-03-10 23:47:00

+2

我这样做,它的工作,但老实说我不漂亮知道为什么 @覆盖 公共无效onPageScrolled(INT位置,浮positionOffset,诠释positionOffsetPixels){ \t查看TabView的= mTab​​Host.getChildAt(位置);如果(tabView!= null){ \t { \t \t final int width = mHorizo​​ntalScroll.getWidth(); \t \t final int scrollPos = tabView.getLeft() - (width - tabView.getWidth())/ 2; \t \t mHorizo​​ntalScroll.scrollTo(scrollPos,0); \t} else { \t \t mHorizo​​ntalScroll.scrollBy(positionOffsetPixels,0); \t \t \t} } – 2012-03-21 22:15:32

相关问题