简体   繁体   English

Android:ScrollView + ViewPager 无法正常工作

[英]Android : ScrollView + ViewPager doesn't work properly

In my android application, I want to implement PagerSlidingTab and ViewPager inside ScrollView .在我的 android 应用程序中,我想在ScrollView实现PagerSlidingTab and ViewPager The layout file is shown below.布局文件如下所示。 I am facing a weird problem in that.我在这方面面临一个奇怪的问题。 It shows only tab strip on top and no content is displayed for ViewPager Fragment content on screen.它仅在顶部显示选项卡条,屏幕上不显示ViewPager Fragment 内容的内容。 I don't know what is the problem with that.我不知道这有什么问题。 If I remove ScreollView from layout then it works perfectly but I want everything inside ScrollView .如果我从布局中删除 ScreollView 那么它可以完美运行,但我想要ScrollView所有内容。 Please help me to solve this issue.请帮我解决这个问题。

Layout :布局 :

<?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>

There was a minor mistake in my code.我的代码中有一个小错误。 I changed the ScrollView code and its working properly.我更改了ScrollView代码并使其正常工作。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

Just need to add android:fillViewport="true" to the ScrollView element.只需将android:fillViewport="true"ScrollView元素即可。 I got the answer from this link: Is it possible to have a ViewPager inside of a ScrollView?我从这个链接得到了答案: Is it possible to have a ViewPager inside a ScrollView?

The simple solution would be to add android:fillViewport="true" to your scroll view but there is a deeper problem in using this layout to consider.简单的解决方案是将 android:fillViewport="true" 添加到您的滚动视图,但在使用此布局时需要考虑一个更深层次的问题。

The problem you are having is that the ScrollView wraps its content and takes "ownership" of the resizing of the elements inside of it.您遇到的问题是 ScrollView 包装了它的内容并获得了对其内部元素大小调整的“所有权”。 So it resizes the Relative layout to only fit in the headers of the PagerSlidingTabStrip cause that is all the information it has when it does the layout for the screen.因此,它会调整相对布局的大小以仅适合 PagerSlidingTabStrip 的标题,因为这是它在为屏幕进行布局时所拥有的所有信息。 The contents of the ViewPager has not been added yet so it doesn't take that into consideration when resizing the relative layout. ViewPager 的内容尚未添加,因此在调整相对布局的大小时没有考虑到这一点。

Suggested solution is to have the scrollview inside of each of the elements that will be inside the viewPager Control.建议的解决方案是将滚动视图包含在 viewPager 控件内的每个元素中。 So you are going to be adding fragments to the ViewPage i assume?所以你打算将片段添加到我假设的 ViewPage 中? just make sure that the ScrollView is the topMost control in each of those layouts.只需确保 ScrollView 是每个布局中的 topMost 控件。 It makes for less unexpected behaviour later on when you want to put for example a listview inside the ViewPager, ListViews and ScrollViews dont like each other and you can only have one or the other you cant have anything else that scrolls inside the ScrollView.当您想在 ViewPager 中放置例如一个列表视图时,它会减少以后的意外行为,ListViews 和 ScrollViews 不喜欢彼此,并且您只能拥有一个或另一个,您不能拥有在 ScrollView 内滚动的任何其他内容。

Hope this has helped you out at least.希望这至少对您有所帮助。 Please ping me if you need more information about this.如果您需要更多信息,请联系我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM