7

Spędziłem trochę czasu próbując wdrożyć to i wykonałem swoją część badań, ale nie mogłem sprawić, żeby działało. W przykładzie autorstwa Chrisa Banesa, cheesesquare, powoduje on przewinięcie paska narzędzi po przewinięciu okna ViewPager. ViewPager jest zawarty bezpośrednio w jego układzie szuflady, tuż przed NaviagtionView.Android - układ koordynatora, układ szuflady i fragmenty

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer_layout" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:fitsSystemWindows="true"> 

    <!-- THIS CONTAINS COORDINATOR LAYOUT with APPBAR LAYOUT + VIEWPAGER --> 
    <include layout="@layout/include_list_viewpager"/> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_height="match_parent" 
     android:layout_width="wrap_content" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header" 
     app:menu="@menu/drawer_view"/> 

</android.support.v4.widget.DrawerLayout> 

Plik include_list_viewpager to:

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabs" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="end|bottom" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_done"/> 

</android.support.design.widget.CoordinatorLayout> 

chciałbym mieć FrameLayout (lub NestedScrollView) zamiast ViewPager. Następnie mogłem dynamicznie ładować fragmenty, gdy użytkownik kliknie elementy układu szuflady, a wszystkie miałyby ładny animowany pasek narzędzi. Do tej pory nie mogę usunąć paska narzędzi Toolbar podczas działania w wywołanych fragmentach. Zastanawiam się, czy to w ogóle możliwe.

Czy ktoś to osiągnął? Każdy wskaźnik jest bardzo doceniany.

+0

pokazać nam swój układ include_list_viewpager –

+0

edytowanej z treścią include_list_viewpager z aplikacji cheesesquare – ticofab

Odpowiedz

6
<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.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/mToolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

     <android.support.design.widget.TabLayout 
      android:id="@+id/tabLayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.widget.DrawerLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <android.support.v4.view.ViewPager 
     android:id="@+id/tab_viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

     <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/drawer_header" 
     app:menu="@menu/drawer"/> 
    </android.support.v4.widget.DrawerLayout> 

</android.support.design.widget.CoordinatorLayout> 

CoordinatorLayout musi być rodzicem wszystkich widoków. DrawerLayout pobiera element layout_behavior. Ponieważ chcesz wchodzić w interakcję z elementami podglądu, musi to być widok z góry na DrawerLayout.

+0

dla mnie ten kod nie działa, ja już dodane brakujące tag CoordinatorLayout, ale moim zdaniem w AppBarLayout robią nic, stojąc tam naprawdę stały. jakieś pomysły? – cV2