我與一個CollapsingToolbarLayout和ViewPager一個測試應用程序。 ViewPager包含三個碎片,其中一個只是一個簡單的RecyclerView。我的問題是,當RecyclerView滾動時,CollapsingToolbarLayout(或更確切地說是AppBarLayout)不會展開/收縮,除非佈局未完全展開,RecyclerView位於頂部並向下滾動。然後CollapsingToolbarLayout也在擴展。讓AppBarLayout/CollapsingToolbarLayout作出反應RecyclerView滾動在ViewPager
注:我已上載測試應用here。
我現在的佈局如下:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true" >
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/mm_recipe_activity_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/cake"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="pin"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.design.widget.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="48dp"
app:tabMode="fixed"
app:tabIndicatorHeight="0dp"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
裏有ViewPager,一個使用一個簡單的TextView包含大量文字和一個與ViewPager兩種類型的片段。碎片全部包裹在NestedScrollView
中,使ViewPager中的滾動功能與其他佈局一致,如建議here和其他地方。下面是這兩個片段佈局,首先是一個用一個簡單的TextView,
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/pink">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lorem"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
,然後一個與RecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<android.support.v7.widget.RecyclerView
android:id="@+id/yellow_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/yellow" />
</android.support.v4.widget.NestedScrollView>
我怎樣才能讓AppBarLayout/CollapsingToolbarLayout反應在滾動這個RecyclerView?
這裏有一些截圖說明我的意思:
向上滾動從初始位置滾動RecyclerView本身,而不是AppBarLayout/CollapsingToolbarLayout。我想在此處向上/向下滾動以正常摺疊/展開它們。
在所述第一圖像,所述CollapsingToolbarLayout已摺疊和RecyclerView是在頂部。如果我滾動RecyclerView 下降在這一點上,我開始擴大CollapsingToolbarLayout,應該是預期的。如果說我是半途而廢和滾動RecyclerView 了,然而,我最終仍向上滾動RecyclerView,如圖所示,第三形象。
獎金問題:什麼是在上面的第三個圖像中的底灰查看?我看不到我已將它添加到任何地方。它也可以在佈局的底部看到,它們只是一個普通的TextView,所以它不是因爲RecyclerView。
非常感謝您的詳細解釋。圍繞RecyclerView移除NestedScrollView會給出預期的行爲。即使在嘗試Romain Guy在博客文章中解釋的內容後,我也無法解決獎勵問題。我也嘗試過'match_parent','wrap_content'和'layout_height =「0dp」'以及'layout_weight =「1」''的各種組合。我在這裏可能會錯過什麼?我還更新了Github上的代碼。 –
這很奇怪,我不能重現你的錯誤。在我的Kitkat 4.4.2中,我無法使用您的更新代碼查看灰色條紋。檢查'android:layout_gravity =「fill_vertical」'是否能以任何方式提供幫助! – Sevle
是的,那很奇怪。使用Android 5.1.1模擬Nexus 5時,灰色條在那裏,但在運行Jelly Bean(4.3)的舊Galaxy S3上,一切正常。運行Android 2.3.3和4.4.4的其他測試模擬器也可以。並且對運行5.1.1 *的Xperia Z1 Compact的最終測試有*灰色條。所以似乎有一些版本問題在手邊。 'android:layout_gravity =「fill_vertical'貌似沒有任何幫助,但這個問題並沒有影響到我的真實應用,不過,就在這個測試中,所以對我來說修復這點並不重要。 –