7

I have a fragment xml in a TabLayout. The TabLayout is in a CollapsingToolbar layout which collapses when scrolling the content of the Fragments in the TabLayout down. I have one fragment where I need a TextView above a recyclerView.

If I have the layout as below taken from this question I asked before:

<LinearLayout>
    <NestedScrollView
      <TextView>
      </TextView>
    </NestedScrollView>
  <View>
  </View>
  <RecyclerView>
  </RecyclerView>
</LinearLayout>

It works ok, until the TextView has so much content in that it fills or takes up most of the screen, the RecyclerView ends up using the remaining space in the view to be displayed:

|------------------|
|<TextView-------->|
|<---------------->|
|<---------------->|
|<---------------->|
|<---------------->|
|</TextView------->|
|<RecyclerView---->|
|</RecyclerView--->|
|__________________|

So the recyclerview is left with minimal space to be viewed. If the Textview takes up the whole screen, the recyclerView just doesn't display.

Taken from this SO Question If the layout is:

<FrameLayout>
    <NestedScrollView
      <TextView>
      </TextView>
    </NestedScrollView>
  <View>
  </View>
  <RecyclerView>
  </RecyclerView>
</FrameLayout>

Only the recyclerView displays and the TextView is just nonexistent.

If the Layout is:

<NestedScrollView>
   <LinearLayout
      <TextView>
      </TextView>
      <View>
      </View>
      <RecyclerView>
      </RecyclerView>
   </LinearLayout>
</NestedScrollView>

The TextView just shows, whether there is content in the RecyclerView or not.

How can I have the TextView scroll out the window enough to reveal the recyclerview so the screen can go from this:

|------------------|
|<TextView-------->|
|<---------------->|
|<---------------->|
|<---------------->|
|<---------------->|
|</TextView------->|
|<RecyclerView---->|
|</RecyclerView--->|
|__________________|

to this:

|------------------|
|<---------------->|
|</TextView------->|
|<RecyclerView---->|
|<---------------->|
|<---------------->|
|<---------------->|
|<---------------->|
|</RecyclerView--->|
|__________________|

My current XML code where only the RecyclerView shows and not the TextView:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="wrap_content"
             android:background="@color/white"
             app:layout_behavior="@string/appbar_scrolling_view_behavior">


  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

      <TextView
          android:id="@+id/item_shipping_shipping_description"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="start|left"
          android:padding="@dimen/margin_16"
          app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </android.support.v4.widget.NestedScrollView>

    <View
        android:id="@+id/line43"
        android:layout_width="match_parent"
        android:layout_height="@dimen/line_height"
        android:background="@color/light_gray"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

  </LinearLayout>


  <android.support.v7.widget.RecyclerView
      android:id="@+id/item_shipping_fragment_recyclerview"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/white"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</FrameLayout>
Community
  • 1
  • 1
Stillie
  • 2,647
  • 6
  • 28
  • 50
  • not sure but try by setting layout height of recycler view to match_parent and framelayout's height is also wrap_content so try by changing it also or both together – Parsania Hardik Feb 24 '16 at 11:26
  • Ok i tried that, set both to match_parent, all the happened was that the RecyclerView was the only View that displayed. I tried changed the FrameLayout to LinearLayout and the only View that was visible was the TextView – Stillie Feb 24 '16 at 11:39
  • you have only one textview then why have you put textview in nestedscrollview? try by removing scrollview – Parsania Hardik Feb 24 '16 at 12:00
  • if i remove it from the nestedscrollview, vertical scrolling wont work in the coordinatorlayout – Stillie Feb 24 '16 at 12:02

4 Answers4

6

Try to add a weight for both layouts.
For example, if you want the TextView part to be less or equal to the RecyclerView part:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/item_shipping_shipping_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="start|left"
                android:padding="@dimen/margin_16"/>
        </android.support.v4.widget.NestedScrollView>

        <View
            android:id="@+id/line43"
            android:layout_width="match_parent"
            android:layout_height="@dimen/line_height"
            android:background="@color/light_gray"/>    
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/item_shipping_fragment_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

This way the TextView containing layout will know that it should wrap the content on one hand but have the same weight the RecyclerView has on the other hand and it will pick the smallest between them.

JeB
  • 11,653
  • 10
  • 58
  • 87
  • But what happens to the content in the textview when it takes up more than half the screen wouldn't it just cut of the bottom of the text? – Stillie Mar 03 '16 at 20:35
  • No, it wouldn't because the TextView is wrapped with NestedScrollView. I've tested this layout, works for me. – JeB Mar 03 '16 at 20:37
  • Ok I will give it a try and let you know – Stillie Mar 03 '16 at 20:37
1

The only fix I had for my issue was to make the list programmatically nested within a nested scrollView. I was not able to use the RecyclerView and nestedScrollview together.

Stillie
  • 2,647
  • 6
  • 28
  • 50
  • Well I was able to do it with the exact layout I gave you. Have you tried it? – JeB Mar 07 '16 at 18:47
  • yes it works, but I then have 2 different scrollable areas on the same section of the screen, and on top of that i have a collapsing toolbar so if i scroll up in either scrollable area it would collapse the toolbar and just make it difficult to browse the screen. Thanks for your help though – Stillie Mar 08 '16 at 06:21
  • So as I answered to your original question, don't you think I deserve the bounty? FYI, bounty starter can't get the bounty. – JeB Mar 08 '16 at 06:27
  • No you didnt answer the question. you had a solution, but it wasnt a solution that worked for me, and yes i know the bounty starter cant get the bounty – Stillie Mar 08 '16 at 06:28
  • 1
    Sorry, maybe I've missed something. The original question was "How can I have the TextView scroll out the window enough to reveal the recyclerview so the screen can go from this:...". Nothing about toolbar. I spent my time to come up with the solution and gave you the exact answer which works. Then you give some other answer and you claim that it solves the problem (which doesn't exist in the question btw). It doesn't seem OK to me, but if you feel that your answer is really better then mine and that my answer doesn't deserve a bounty - good luck and best wishes. – JeB Mar 08 '16 at 07:15
  • 1
    Hey @binyan don't worry about that, I give +1 for your working solution and your time ;) – Gueorgui Obregon Mar 09 '16 at 15:20
0

Instead of frame layout take linearlayout with verticle orientation and put recyclerview in another linearlayout:

<LinearLayout orientation=verticle >


  <LinearLayout
      >

    <android.support.v4.widget.NestedScrollView>

      <TextView/>

    </android.support.v4.widget.NestedScrollView>

    <View
        />

  </LinearLayout>

   <LinearLayout>
       <android.support.v7.widget.RecyclerView/>
   </LinearLayout>

</LinearLayout>

layout height of both child layout should be set to wrap_content

Parsania Hardik
  • 4,593
  • 1
  • 33
  • 33
  • Hi, thanks for this, but that didnt work either, the TextView i am testing with now has enough content in to scroll but when it reaches the end of the TextView the RecyclerView is not visible – Stillie Feb 24 '16 at 12:19
  • This isn't a java code issue due to the fact that everything works fine if the TextView is nice and short. The RecyclerView has enough room to display – Stillie Feb 24 '16 at 12:25
  • I want to test it in my pc – Parsania Hardik Feb 24 '16 at 12:36
  • all you need to do is have a TextView full of text to the point where it goes off the screen and then below it have a recyclerview with scrollable data in – Stillie Feb 24 '16 at 12:58
0

You can use CoordinatorLayout to hide TextView on ReciclerView scrolling:

Here is easy tutorial.

Vitaliy A
  • 3,651
  • 1
  • 32
  • 34