Ich habe eine MainActivity
mit einem FrameLayout
Platzhalter, wo ich in/out Fragmente basierend auf der NavigationView-Auswahl wechseln. Ich möchte in der Lage sein, die MainActivity Toolbar vom Bildschirm scrollen zu lassen, wenn sie ein Scroll-Verhalten in irgendein Fragment findet, das es enthält (sogar verschachtelte Fragmente).AppBarLayout Scroll-Verhalten funktioniert nicht für verschachtelte Fragmente
Es funktioniert gut für HomeFragment (1)
, die nur eine SwipeRefreshLayout
und eine RecyclerView
enthält.
jedoch, für die ViewPagerFragment (2)
, die eine TabLayout
und ViewPager
mit FragmentStatePagerAdapter
und Kind Fragmente des Typs enthält ViewPagerItemFragment (3)
, scheitert es scroll Ereignisse auf dem ViewPagerItemFragment's
enthält RecyclerView
zu registrieren.
Komplette Layouts (ohne Thema oder unnötige Attribute) sind unten.
layout_activity_main:
<android.support.v4.widget.DrawerLayout
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.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:layout_scrollFlags="scroll|enterAlways">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Spinner
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:layout_width="@dimen/drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"/>
</android.support.v4.widget.DrawerLayout>
layout_home_fragment (1):
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
layout_fragment_viewpager (2):
<LinearLayout 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:orientation="vertical">
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="@dimen/tab_strip_height"/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
fragment_viewpager_item (3):
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>