[英]Android : ScrollView + ViewPager doesn't work properly
在我的 android 应用程序中,我想在ScrollView
实现PagerSlidingTab and ViewPager
。 布局文件如下所示。 我在这方面面临一个奇怪的问题。 它仅在顶部显示选项卡条,屏幕上不显示ViewPager
Fragment 内容的内容。 我不知道这有什么问题。 如果我从布局中删除 ScreollView 那么它可以完美运行,但我想要ScrollView
所有内容。 请帮我解决这个问题。
布局 :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.danzim.diamond.classes.KenBurnsView
android:id="@+id/newsHeaderKenburn"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentTop="true"
android:background="@color/black" />
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/newsTabs"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_below="@+id/newsHeaderKenburn"
android:background="@drawable/background_news_tab"
app:pstsDividerColor="@color/transparent"
app:pstsIndicatorColor="@android:color/white"
app:pstsTextAllCaps="true" />
<android.support.v4.view.ViewPager
android:id="@+id/newsPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/newsTabs" />
</RelativeLayout>
</ScrollView>
我的代码中有一个小错误。 我更改了ScrollView
代码并使其正常工作。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
只需将android:fillViewport="true"
到ScrollView
元素即可。 我从这个链接得到了答案: Is it possible to have a ViewPager inside a ScrollView?
简单的解决方案是将 android:fillViewport="true" 添加到您的滚动视图,但在使用此布局时需要考虑一个更深层次的问题。
您遇到的问题是 ScrollView 包装了它的内容并获得了对其内部元素大小调整的“所有权”。 因此,它会调整相对布局的大小以仅适合 PagerSlidingTabStrip 的标题,因为这是它在为屏幕进行布局时所拥有的所有信息。 ViewPager 的内容尚未添加,因此在调整相对布局的大小时没有考虑到这一点。
建议的解决方案是将滚动视图包含在 viewPager 控件内的每个元素中。 所以你打算将片段添加到我假设的 ViewPage 中? 只需确保 ScrollView 是每个布局中的 topMost 控件。 当您想在 ViewPager 中放置例如一个列表视图时,它会减少以后的意外行为,ListViews 和 ScrollViews 不喜欢彼此,并且您只能拥有一个或另一个,您不能拥有在 ScrollView 内滚动的任何其他内容。
希望这至少对您有所帮助。 如果您需要更多信息,请联系我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.