Thursday, March 10, 2016

NestedScrollview + TextView + RecyclerView

Leave a Comment

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> 

4 Answers

Answers 1

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.

Answers 2

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.

Answers 3

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

Answers 4

You can use CoordinatorLayout to hide TextView on ReciclerView scrolling:

Here is easy tutorial.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment