1

Trying to build layout in which the activity has Coordinator layout and Appbar layout. The appbar layout has tab layout, which is connected to a view pager. In the view pager, the first fragment has another coordinator layout and an appbar layout. When the scroll happens in appbar layout, I want the Activity's appbar layout also to scroll.

Activity's 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>

The fragment's 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>

When the appbar scrolls in the fragment, I want the activity's appbar to scroll also. I have used the NestedCoordinatorLayout as suggested in CoordinatorLayout inside another CoordinatorLayout Please let me know how to achieve that Thanks!

Anup K
  • 61
  • 3

0 Answers0