9

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?

fab
  • 790
  • 6
  • 10
  • 3
    Check out this guide http://inthecheesefactory.com/blog/android-design-support-library-codelab/en and see if it helps – EpicPandaForce Jun 24 '15 at 17:34
  • 2
    "it does not work" means nothing. – Marcin Orlowski Jun 24 '15 at 17:34
  • you shouldn't have one view that has own scroll view inside another view that has own scroll view too, but see if this answer helps you [nested scroll view](http://stackoverflow.com/questions/4490821/scrollview-inside-scrollview) – Jhonatas Jun 24 '15 at 17:58
  • I want to use fragments to inject them in the framelayout, do I need to create the AppBarLayout structure in all my fragments instead to be in the Activity layout? – fab Jun 24 '15 at 20:40
  • You shouldn't be adding a RecyclerView inside of a NestedScrollView (only use one or the other). I'd recommend using something like a ViewPager to nest your Fragments. Here is an example: https://github.com/blackcj/DesignSupportExample – blackcj Jun 24 '15 at 22:29
  • @blackcj but If don't want to use a ViewPager, just want to use framelayout to inject different fragments with RecyclerView inside or not, is there no solution in this case? – fab Jun 25 '15 at 06:42

1 Answers1

11

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.

blackcj
  • 3,651
  • 2
  • 25
  • 23
  • 1
    what is the final solution dat u come across,can u throw some light,i am stuck with similar case, – Srishti Roy Jul 07 '15 at 10:18
  • 2
    Thank you. You are my saviour. This solution should be marked as the correct solution, it worked for me. For those people who are curious, you do not need to create the linearLayout inside if you do not need to put anything there. – Simon Jul 18 '15 at 18:21
  • @blackcj the frame layout doesn't implement layout behaviour for the CoordinatorLayout, I think – fab Jul 23 '15 at 10:11
  • @fab This works as long as the first child of the FrameLayout is a NestedScrollView or RecyclerView. The CoordinatorLayout is able to identify the correct target. – blackcj Jul 23 '15 at 14:45
  • I am inflating fragments in frame layout,,,,my toolbar gets stuck most of the times...any solutions? – Rishabh Srivastava Jul 29 '15 at 06:43
  • YESSS!!! Thank you, after hours searching, THIS is the solution to replace different kind of fragments – Luchi Valles Sep 10 '15 at 08:52