John
John

Reputation: 485

RecyclerView doesnot scroll when not nested in NestedScrollView but is in the same LinearLayout

I am using the following layout, but unable to get the RecyclerView to scroll(it is not visible on the screen when using this layout, scrolling stops till the NestedScrollView).

I can scroll up to the NestedScrollView and the CollapsingToolbar to collapse, if I remove the entire NestedScrollView then I get the RecyclerView to scroll.

If I keep the linear layout without the NestedScrollView, only the RecyclerView scrolls, the rest of the layout is fixed.

I have also added app:layout_behavior="@string/appbar_scrolling_view_behavior" to the RecyclerView, and have kept the RecyclerView out of the NestedScrollView.

If I add the RecyclerView inside the NestedScrollView, the RecyclerView does not appear.

<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:fitsSystemWindows="true"
    tools:context="com.example.MainFragment">

    <!-- android.support.design.widget.AppBarLayout here
         with a android.support.design.widget.CollapsingToolbarLayout
    -->

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

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

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

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp">

                    <!-- more layout code here -->
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/large_text"/>

                </RelativeLayout>



                <View
                    android:id="@+id/separator"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/colorAccent" />

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


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerViewListOfData"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:listitem="@layout/recycler_view"
           app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    </LinearLayout>

Upvotes: 0

Views: 315

Answers (2)

John
John

Reputation: 485

I solved it by nesting RecyclerView inside the NestedScrollView and updating the support library for recyclerview

I was using com.android.support:recyclerview-v7:23.0.1

as of Android Support Library, revision 23.2.0 (February 2016) (refer revision archive here)

Changes for v7 RecyclerView library:

RecyclerView now has an opt-in feature called AutoMeasure which allows RecyclerView.LayoutManager to easily wrap content or handle various measurement specifications provided by the parent of the RecyclerView. It supports all existing animation capabilities of the RecyclerView.

If you have a custom RecyclerView.LayoutManager, call setAutoMeasureEnabled(true) to start using the new AutoMeasure API. All built-in RecyclerView.LayoutManager objects enable auto-measure by default.

RecyclerView.LayoutManager no longer ignores some RecyclerView.LayoutParams settings, such as MATCH_PARENT in the scroll direction.

Note: These lifted restrictions may cause unexpected behavior in your layouts. Make sure you specify the correct layout parameters.

Using com.android.support:recyclerview-v7:23.4.0 solves the problem of nested recyclerview not appearing in nested scroll view

Upvotes: 1

Farid
Farid

Reputation: 2562

Ok, if you want to add RecyclerView inside NestedScrollView add this line into RecyclerView in xml file app:layoutManager="LinearLayoutManager".

Example

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/your_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layoutManager="LinearLayoutManager"/>

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

Then in your SomeActivity.java file where you populate RecyclerView put this line recyclerView.setNestedScrollingEnabled(false); before you setting adapter to RecyclerView.

Example

RecyclerView recyclerView=(RecyclerView)findViewById(R.id.your_recyclerview);
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setAdapter(yourAdapter);

Upvotes: 1

Related Questions