Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AppBarLayout, NestedScrollView, FrameLayout, what is the deal?

I have the following code in my activity:

    <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="160dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <FrameLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    </android.support.v4.widget.NestedScrollView>

I want to add a fragment with a RecyclerView inside, in the frame layout ("main_content" id layout), but in this case, this doesn't work.

What is the problem? do you know some example?

like image 453
fab Avatar asked Jun 24 '15 17:06

fab


1 Answers

The collapsing toolbar should have a single scrolling target. When you replace the content, you should be replacing the scrolling container, not nesting them. For example:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_container"
        android:background="@color/colorPrimary"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <!-- Your content here -->
            </LinearLayout>
        </android.support.v4.widget.NestedScrollView>
    </FrameLayout>

    <include
        layout="@layout/toolbar"/>

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

You can then call:

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

PageRecycle recycle = PageRecycle.create();
ft.replace(R.id.main_container, recycle);
ft.commit();

Where PageRecycle has a RecyclerView (or NestedScrollView) as the root node. This will ensure the coordinator layout has a single scrolling view to use as the target.

like image 188
blackcj Avatar answered Oct 14 '22 16:10

blackcj