2

I have a header at the top and a recyclerview underneath it. Like so:

<LinearLayout> 
    <LinearLayout>
        <ImageView/>
        <TextView/>
        <TextView/>
     </LinearLayout>
    <RecyclerView/>
</LinearLayout>

I also have a view with a header and two recyclerviews. Like so:

<LinearLayout>
     <LinearLayout>
        <ImageView/>
        <TextView/>
        <TextView/>
     </LinearLayout>
     <Recyclerview/>
     <Recyclerview/>
</LinearLayout>

When I scroll up on the recyclerview on the first layout, the header stays there. I want the header to go up and out of the view as I scroll down the recyclerview.

On the second layout, each list is short and could fit on the screen individually, but together the second one goes offscreen (i'm using this layout manager to make the layout wrap content). This would be fine, however when you scroll up on the first recyclerview, it only tries to scroll itself (which is can't because there's nothing to scroll) so you can't scroll to see what's offscreen. You also can't scroll on the header to go offscreen.

In both cases, how can I get the recyclerview to scroll with its parent?

Caleb Lewis
  • 523
  • 6
  • 20
  • Short answer: you shouldn't make RecyclerView scroll together with its parent. The second one can be fixed by combining two recyclerViews into one (one RecyclerView can show multiple different viewTypes, so it shouldn't be a problem) – Konstantin Loginov Dec 27 '15 at 16:29
  • So each screen that contains a linear recyclerview + other views has consist of only that recyclerview where it is composed of all of the views that you want to put on that screen using different view types? Is that the only way to do things? @KonstantinLoginov – Caleb Lewis Dec 27 '15 at 16:40
  • 1
    Of course, there're multiple ways of dealing with it. :-) There're also anti-patterns like disabling Scroll in ListView and putting it into ScrollView (it's bad due to memory usage - it's not recycling views then). In general, I'd say, according guidelines there should be 1 control, which has vertical scrolling on the page. Android also has CoordinatorLayout&CollapsingToolbarLayout for animating collapsing header. It might be applicable for your first question (read more http://antonioleiva.com/collapsing-toolbar-layout/ here) – Konstantin Loginov Dec 27 '15 at 16:55
  • @KonstantinLoginov Thanks for the advice and link! It's exactly what I needed. – Caleb Lewis Dec 27 '15 at 17:41
  • Great! Then if you don't mind - I'll convert the comment into an actual answer :-) – Konstantin Loginov Dec 27 '15 at 17:45

2 Answers2

1

In the first case, you can use CollapsingToolBarLayout. There're number of nice tutorials/examples about it:

In the second case, I'd suggest to replace 2 RecyclerViews by 1 with different viewTypes for the sake of keeping views recycled during scrolling.

Community
  • 1
  • 1
Konstantin Loginov
  • 15,802
  • 5
  • 58
  • 95
-1
<NestedScrollView>
 ...your views here...
 <RecyclerView
   android:NestedScrollViewEnabled="false"/>
</NestedScrollView>