1

嘗試構建佈局,其中活動具有協調器佈局和應用程序佈局。應用程序欄佈局具有選項卡布局,該佈局連接到視圖尋呼機。在視圖尋呼機中,第一個片段有另一個協調器佈局和一個appbar佈局。當滾動發生在appbar佈局中時,我希望Activity的appbar佈局也可以滾動。嵌套協調器佈局和應用程序佈局

活動的XML

<?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:id="@+id/coord" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

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

      <android.support.design.widget.CollapsingToolbarLayout 
       android:id="@+id/collapse_layout" 
       android:layout_width="match_parent" 
       android:layout_height="200dp" 
       app:layout_scrollFlags="scroll|snap|enterAlwaysCollapsed"> 


       <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:scaleType="centerCrop" 
        android:src="@drawable/material_flat" 
        app:layout_collapseMode="parallax" /> 


       <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" 
        style="@style/ToolBarWithNavigationBack" 
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize" 
        android:background="@android:color/transparent" 
        app:layout_collapseMode="pin"></android.support.v7.widget.Toolbar> 
      </android.support.design.widget.CollapsingToolbarLayout> 

      <LinearLayout 
       android:id="@+id/tab_container" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:background="@android:color/white" 
       android:clipToPadding="true" 
       android:gravity="center" 
       android:orientation="vertical" 
       android:paddingTop="10dp" 
       app:layout_scrollFlags="scroll|enterAlways|snap"> 

       <android.support.design.widget.TabLayout 
        android:layout_width="fill_parent" 
        android:layout_height="?attr/actionBarSize" 
        android:layout_gravity="bottom" 
        app:tabIndicatorColor="#BDBDBD" 
        app:tabIndicatorHeight="4dp" 
        app:tabSelectedTextColor="?android:attr/textColorPrimaryInverse" 
        app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" /> 
      </LinearLayout> 

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

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

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

片段的XML

  <?xml version="1.0" encoding="utf-8"?> 
      <NestedCoordinatorLayout 
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:background="@android:color/background_light"> 

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


    <LinearLayout 
     android:id="@+id/home" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#e3e6e8" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:padding="20dp" 
     app:layout_scrollFlags="scroll|enterAlwaysCollapsed|snap"> 

     <android.support.v7.widget.CardView 
      card_view:cardCornerRadius="4dp" 
      android:layout_width="match_parent" 
      android:layout_height="150dp" 
      android:layout_margin="24dp"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@drawable/shade_gradient" /> 

     </android.support.v7.widget.CardView> 

    </LinearLayout> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="15dp" /> 
</android.support.design.widget.AppBarLayout> 

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

    </NestedCoordinatorLayout> 

當片段appbar卷軸,我想活動的appbar也滾動。 我已經使用了CoordinatorLayout inside another CoordinatorLayout 建議的NestedCoordinatorLayout請讓我知道如何實現那 謝謝!

回答

0

嘗試嵌套滾動型相對佈局內像下面

  <?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
xmlns:app="http://schemas.android.com/tools" 
android:background="@android:color/background_light"> 

<NestedCoordinatorLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 

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


    <LinearLayout 
     android:id="@+id/home" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#e3e6e8" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:padding="20dp" 
     app:layout_scrollFlags="scroll|enterAlwaysCollapsed|snap"> 

     <android.support.v7.widget.CardView 
      card_view:cardCornerRadius="4dp" 
      android:layout_width="match_parent" 
      android:layout_height="150dp" 
      android:layout_margin="24dp"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@drawable/shade_gradient" /> 

     </android.support.v7.widget.CardView> 

    </LinearLayout> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="15dp" /> 
</android.support.design.widget.AppBarLayout> 

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

    </NestedCoordinatorLayout> 

    </RelativeLayout> 
+0

它沒有工作 –