TabWidget wrapped in HorizontalScrollView doesn#39;t scroll with ViewPager(包装在 HorizontalScrollView 中的 TabWidget 不使用 ViewPager 滚动)
问题描述
我不得不使用 TabHost
代替 ActionBarTabs
并使它们可滚动,我将 TabWidget
包装在HorizontalScrollView
,但 HorizontalScrollView
不会按照 ViewPager
自行滚动.我已经尝试以几种不同的方式使用 scrollTo 和 fullScroll ,但它并没有改变任何东西.我需要做什么才能使其正常工作?
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);
}
推荐答案
调用.scrollTo(x,y)后调用
方法..refreshDrawableState()
会改变水平滚动视图
The horizontal scroll view will change when you call .refreshDrawableState()
after calling the .scrollTo(x,y)
method.
要注意的另一件事是 .scrollTo(x,y)
滚动以使 x
位于屏幕的左侧.您可能需要对选项卡的坐标和水平滚动视图的宽度进行一些数学运算才能正确定位.你不能调用 .scrollTo(position,0)
并让它按照你想要的方式工作(除非你的标签是 1 像素宽).
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).
这篇关于包装在 HorizontalScrollView 中的 TabWidget 不使用 ViewPager 滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:包装在 HorizontalScrollView 中的 TabWidget 不使用 ViewPager 滚动
基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01