Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NestedScrollview + TextView + RecyclerView

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>
like image 258
Stillie Avatar asked Feb 24 '16 10:02

Stillie


2 Answers

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.

like image 69
JeB Avatar answered Nov 09 '22 16:11

JeB


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.

like image 21
Stillie Avatar answered Nov 09 '22 15:11

Stillie