【问题标题】:Android : ScrollView + ViewPager doesn't work properlyAndroid : ScrollView + ViewPager 不能正常工作
【发布时间】:2015-01-06 10:11:19
【问题描述】:

在我的 android 应用程序中,我想在 ScrollView 中实现 PagerSlidingTab and ViewPager。布局文件如下所示。我正面临一个奇怪的问题。它仅在顶部显示标签条,屏幕上不显示 ViewPager 片段内容的内容。我不知道那有什么问题。如果我从布局中删除 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>

【问题讨论】:

  • 您是否尝试过RelativeLayout 大小包裹内容而不是匹配父项?
  • @Haketo 是的,我试过了。它不起作用

标签: android android-fragments android-viewpager android-scrollview pagertabstrip


【解决方案1】:

我的代码中有一个小错误。我更改了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 of a ScrollView?

【讨论】:

    【解决方案2】:

    简单的解决方案是将 android:fillViewport="true" 添加到您的滚动视图中,但使用此布局需要考虑更深层次的问题。

    您遇到的问题是 ScrollView 包装了它的内容并“拥有”了它内部元素大小的调整。因此,它调整了相对布局的大小,使其仅适合 PagerSlidingTabStrip 的标题,因为这是它在为屏幕进行布局时拥有的所有信息。 ViewPager 的内容尚未添加,因此在调整相对布局大小时不会考虑到这一点。

    建议的解决方案是将滚动视图放在 viewPager 控件内的每个元素内。所以你要向我假设的 ViewPage 添加片段?只需确保 ScrollView 是每个布局中的 topMost 控件。当你想在 ViewPager 中放置一个列表视图时,它可以减少意外的行为,ListViews 和 ScrollViews 彼此不喜欢,你只能有一个或另一个你不能在 ScrollView 内滚动任何其他内容。

    希望这至少对您有所帮助。如果您需要有关此的更多信息,请联系我。

    【讨论】:

    • yaa,我得到了那个解决方案,但我必须将 viewpager 放在 ScrollView 内,因为我想要滚动整个页面的功能,该页面可以有任何类似 ViewPager 和其他控件的功能。所以我在 ViewPager 之外使用 ScrollView。感谢您的重播。
    • 好吧,很酷,只是想我会指出,您将无法在稍后将在视图寻呼机中使用的片段中使用任何其他使用滚动功能的列表视图 :) .该 fillViewport = true 部分将至少确保滚动视图将填充屏幕上的所有可用空间,而不是仅仅重新调整内容的大小
    • 因此,当您在滚动视图中有一个带有 wrap_content 的 viewpager 时,没有办法让整个视图可滚动?
    • @AdrianoCelentano 很抱歉迟到了..但是当使用 Scrollview 作为带有 viewpager 的片段内的根视图时,我遇到了同样的问题..你找到解决这个问题的方法了吗?
    猜你喜欢
    • 1970-01-01
    • 2019-01-04
    • 2013-11-24
    • 2016-07-19
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多