2

I have a ViewPager inside an Activity which shows two Fragments.

The activity has a TabLayout and a CollapsingToolbarLayout. The CollapsingToolbarLayout is nested inside the AppBarLayout with an ImageView to produce a parallax effect. The TabLayout is outside the AppBarLayout inside a LinearLayout with the ViewPager. Then there is a FrameLayout as well to show another Fragment over the ViewPager when the user clicks a button to show that Fragment.

The problem is that when you scroll the RecyclerView inside the Fragment that overlays on top of the ViewPager the Activity also scrolls and does the parallax effect. Essentially it scrolls both the RecyclerView inside the Fragment and the content inside the ViewPager as well.

Is there a way to stop scrolling for the CollapsingToolbarLayout and only scroll content in the overlay Fragment when the user sees that Fragment?

This is what my XML Hierarchy looks like:

<android.support.design.widget.CoordinatorLayout>

<android.support.design.widget.AppBarLayout>

    <android.support.design.widget.CollapsingToolbarLayout
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            app:layout_collapseMode="parallax"/>

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

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

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

<LinearLayout
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >

    <android.support.design.widget.TabLayout />
    <!-- Two fragments inside ViewPager each containing a RecyclerView -->
    <android.support.v4.view.ViewPager />

</LinearLayout>

<!-- User Clicks a button and fragment is added to this framelayout -->
<!-- This fragment overlays on top of the entire Activity layout -->
<!-- This fragment also contains a recyclerview -->
<FrameLayout/>


</android.support.design.widget.CoordinatorLayout>
AndyRoid
  • 4,754
  • 8
  • 33
  • 70
  • If you don't want your activity to scroll your appbar then remove the layout_behavior attribute for your LinearLayout. I assume your fragment FrameLayout contains that layout_behavior? – McGuile Jul 08 '15 at 20:22
  • Do you want your FrameLayout to be displayed full screen (covering the Toolbar) or below the Toolbar? If you move the FrameLayout outside of the CoordinatorLayout (covering the Toolbar), you can then override the on touch listener to prevent the CoordinatorLayout from scrolling in the background. – blackcj Jul 08 '15 at 20:26
  • @McGuile that makes sense, maybe I can programmatically remove the scrolling behavior when the Fragment overlays the Activity – AndyRoid Jul 08 '15 at 20:27
  • @blackcj below the toolbar – AndyRoid Jul 08 '15 at 20:27

2 Answers2

12

The CoordinatorLayout and AppBarLayout operate via onNestedScroll callbacks from children. If you have a scrolling child, you can stop it from effecting its parent/s by disabling this callback:

recyclerView.setNestedScrollingEnabled(false);
tachyonflux
  • 19,305
  • 6
  • 43
  • 65
  • Posted this earlier on: http://stackoverflow.com/questions/30779123/need-to-disable-expand-on-collapsingtoolbarlayout-for-certain-fragments/. But it was largely ignored. Wondering if it actually solves your issue. If not, perhaps you can find your answer in that question. :3 – tachyonflux Aug 22 '15 at 18:56
  • Thanks, I'll test it out but your answer is very short. – AndyRoid Aug 23 '15 at 01:19
  • Can you give me a detailed answer with thorough explanation of WHY this occurs? I will award you the bounty. – AndyRoid Aug 23 '15 at 01:24
-4

Try a nestedscrollview instead of linearlayout.

  • 2
    This answer definitely deserves a vote down, not only did you not consider my application specific requirements but suggested a completely different approach one that I am already familiar and is usually a fairly basic example, for example most new design support library example start with a `NestedScrollView`. What if I wanted to not let the LinearLayout container scroll off the screen? Your answer is also 6 words, vague and obscure it does not help me nor anyone else viewing this question. – AndyRoid Aug 16 '15 at 22:57
  • Nicely articulated, sir. – Mark Miller Sep 11 '15 at 22:00