0

I have a slight issue with scrolling on a screen. Layout is as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  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">

    <LinearLayout
      android:id="@+id/my_account_parent_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_behavior="@string/appbar_scrolling_view_behavior">

       <!--- Views omitted --->
    </LinearLayout>
  </android.support.v4.widget.NestedScrollView>

  <android.support.v7.widget.RecyclerView
    android:id="@+id/my_account_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</LinearLayout>

The issue is that the recyclerview cannot be in a Scrollview/NestedScrollView thats why its outside the scrollview. Now i need the views that are wrapped in the LinearLayout which is wrapped around the the NestedScrollView to scroll upwards when the user scrolls on any of the views (RecyclerView and All the others above it). Even though the other views have a scrollview wrapped around them, they dont realise that they can scroll out the screen even though the recyclerview has content outside the screen?

Thanks

Stillie
  • 2,647
  • 6
  • 28
  • 50

2 Answers2

0

Yes, it is not a good practice to add RecyclerView inside ScrollView but you can add a custom header to the recyclerView.

Follow this post here

Its a good SO post for adding a header to your RecyclerView.

So here you can add the LinearLayout which is inside your ScrollView as the first child (header) of your recyclerview and then i suppose it should work as you desire it to!

Which will make you final layout for that screen only this:

  <android.support.v7.widget.RecyclerView
    android:id="@+id/my_account_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

Where the LinearLayout will be inflated in the adapter of the RV as per the post!

Community
  • 1
  • 1
MadScientist
  • 2,134
  • 14
  • 27
0

You can use CoordinatorLayout and app:layout_scrollFlags . When recyclerview scroll up, layout inside AppBarLayout will scroll up.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="0dp">

        <LinearLayout
      android:id="@+id/my_account_parent_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_scrollFlags="scroll|enterAlways">


    </LinearLayout>

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

    <android.support.v7.widget.RecyclerView
    android:id="@+id/my_account_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</android.support.design.widget.CoordinatorLayout>
RoShan Shan
  • 2,924
  • 1
  • 18
  • 38