[英]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.