Recyclerview inside ScrollView not scrolling smoothly
Asked Answered
C

18

192

For my app I am using a RecyclerView inside a ScrollView where the RecyclerView has a height based on its content using this library. Scrolling is working but it's not working smoothly when I scroll over the RecyclerView. When I scroll over the ScrollView itself it is scrolling smoothly.

The code I am using to define the RecyclerView:

LinearLayoutManager friendsLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext(), android.support.v7.widget.LinearLayoutManager.VERTICAL, false);
mFriendsListView.setLayoutManager(friendsLayoutManager);
mFriendsListView.addItemDecoration(new DividerItemDecoration(getActivity().getApplicationContext(), null));

The RecyclerView in the ScrollView:

<android.support.v7.widget.RecyclerView
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/friendsList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
Collyrium answered 15/10, 2015 at 8:27 Comment(3)
this solution work for me: https://mcmap.net/q/136696/-making-recyclerview-fixed-height-and-scrollable thank youPronghorn
@tahaDev what exactly not working in your case please elaborate more on that. Also, it seems like no provided solutions worked in your case, is it so?Classless
Use androidx.constraintlayout.widget.ConstraintLayout which will solve your problem without any complex implementationsLowery
A
399

Try doing:

RecyclerView v = (RecyclerView) findViewById(...);
v.setNestedScrollingEnabled(false);

As an alternative, you can modify your layout using the support design library. I guess your current layout is something like:

<ScrollView >
   <LinearLayout >

       <View > <!-- upper content -->
       <RecyclerView > <!-- with custom layoutmanager -->

   </LinearLayout >
</ScrollView >

You can modify that to:

<CoordinatorLayout >

    <AppBarLayout >
        <CollapsingToolbarLayout >
             <!-- with your content, and layout_scrollFlags="scroll" -->
        </CollapsingToolbarLayout >
    </AppBarLayout >

    <RecyclerView > <!-- with standard layoutManager -->

</CoordinatorLayout >

However this is a longer road to take, and if you are OK with the custom linear layout manager, then just disable nested scrolling on the recycler view.

Edit (4/3/2016)

The v 23.2 release of the support libraries now includes a factory “wrap content” feature in all default LayoutManagers. I didn’t test it, but you should probably prefer it to that library you were using.

<ScrollView >
   <LinearLayout >

       <View > <!-- upper content -->
       <RecyclerView > <!-- with wrap_content -->

   </LinearLayout >
</ScrollView >
Aerolite answered 15/10, 2015 at 8:29 Comment(14)
To add to this answer: setNestedScrollingEnabled(false) only worked when I switched out the ScrollView for a NestedScrollView instead.Olga
For me, setNestedScrollingEnabled(false) gave me back smooth scrolling with my RecyclerView inside of a ScrollView - Thanks! But I still don't understand why that works...? What does it really mean to set nested scrolling false?Lord
Note that android:nestedScrollingEnabled="false" only works for API 21+ but v.setNestedScrollingEnabled(false) is OK for <21.Sihonn
This seems to work fine and saved me hours of work. Thanks. Is there any downside to using this solution? Because there are a lot of comments out there stating this is not a good solution, that you should not put scrolling views inside one another.Heptagon
@Heptagon these comments are probably very old. Since nested scrolling was introduced, it is ok to nest scrolling views.Aerolite
For future reference, if anybody is experiencing RecyclerView wrap_content issue inside ScrollView that only happens on marshmallow/nougat (API 23, 24) devices, check my workaround at https://mcmap.net/q/93220/-recyclerview-inside-scrollview-is-not-workingAeroballistics
@MicroR another problem pops up, nested view content will be scrolled out by recyclerview every timeObviate
A downside to this solution I'm experiencing right now is that the RecyclerView will not receive events in it's onScrollListener. Which I need because I want to fetch more data when I have only a certain amount of items in the recyclerCindicindie
@Daniel if the recycler is wrap_content, it means it can't scroll. Maybe use a special item, like a progress bar, and fetch data onBindViewHolder of that particular item.Aerolite
setNestedScrollingEnabled(false) just make the recycleview fast..still dont know what happen..hahaPhotothermic
v.setNestedScrollingEnabled(false); This line helped a lot..ThanksSynchronous
setNestedScrollingEnabled(false) Solved my issue of scrollPernell
This solution solved the scroll effect, I have continuation problem from this. I want to have drag option for the RecyclerView. I can only drop the item in the visible items on the screen. Not able to drop it on the items that are not visible in the list. I can achieve it if I remove NestedScrollView. I tried requestDisallowInterceptTouchEvent in onTouchEvent but no luck. Anyone had the same problem?Delastre
Just added nestedScrollingEnabled="false" in recyclerview It worked for me. Thanks for ur support.Testy
S
88

I only needed to use this:

mMyRecyclerView.setNestedScrollingEnabled(false);

in my onCreateView() method.

Thanks a lot!

Shoshone answered 30/8, 2016 at 16:47 Comment(0)
B
31

You can use this way either :

Add this line to your recyclerView xml file :

android:nestedScrollingEnabled="false"

Or in java code :

RecyclerView.setNestedScrollingEnabled(false);

Hope this helped .

Bolte answered 21/12, 2016 at 17:3 Comment(1)
requires Api 21+Diahann
P
11

You can try with both the ways with XML and programmatically. But the issue you may face is (below API 21) by doing it with XML will not work . So it's better to set it programmatically in your Activity / Fragment.

XML code:

<android.support.v7.widget.RecyclerView
      android:id="@+id/recycleView"
      android:layout_width="match_parent"
      android:visibility="gone"
      android:nestedScrollingEnabled="false"
      android:layout_height="wrap_content"
      android:layout_below="@+id/linearLayoutBottomText" /> 

Programmatically:

 recycleView = (RecyclerView) findViewById(R.id.recycleView);
 recycleView.setNestedScrollingEnabled(false);
Pick answered 20/4, 2017 at 11:39 Comment(0)
T
7

Using Nested Scroll View instead of Scroll View solved my problem

<LinearLayout> <!--Main Layout -->
   <android.support.v4.widget.NestedScrollView>
     <LinearLayout > <!--Nested Scoll View enclosing Layout -->`

       <View > <!-- upper content --> 
       <RecyclerView >


     </LinearLayout > 
   </android.support.v4.widget.NestedScrollView>
</LinearLayout>
Tephrite answered 10/10, 2018 at 11:3 Comment(0)
S
5

I had similar issues (I tried to create a nested RecyclerViews something like Google PlayStore design). The best way to deal with this is by subclassing the child RecyclerViews and overriding the 'onInterceptTouchEvent' and 'onTouchEvent' methods. This way you get complete control of how those events behave and eventually scrolling.

Sumerlin answered 22/9, 2016 at 21:43 Comment(0)
B
5

Replacing ScrollView with NestedScrollView resulted into smooth scrolling to the bottom.

Bairn answered 16/10, 2019 at 8:55 Comment(0)
M
5

Every answer is same here. and i already used what everyone is suggested. Then i found that NestedScrollView is faster then ScrollView so

use

<androidx.core.widget.NestedScrollView

Instead of

<ScrollView

And use this as usual

recycleView.setNestedScrollingEnabled(false);
Miler answered 28/11, 2020 at 11:25 Comment(0)
P
3

Summary of all answers (Advantages & Disadvantages)

For single recyclerview

you can use it inside Coordinator layout.

Advantage - it will not load entire recyclerview items. So smooth loading.

Disadvantage - you can't load two recyclerview inside Coordinator layout - it produce scrolling problems

reference - https://mcmap.net/q/134406/-recyclerview-inside-scrollview-not-scrolling-smoothly

For multiple recylerview with minimum rows

you can load inside NestedScrollView

Advantage - it will scroll smoothly

Disadvantage - It load all rows of recyclerview so your activity open with delay

reference - https://mcmap.net/q/134406/-recyclerview-inside-scrollview-not-scrolling-smoothly

For multiple recylerview with large rows(more than 100)

You must go with recyclerview.

Advantage - Scroll smoothly, load smoothly

Disadvantage - You need to write more code and logic

Load each recylerview inside main recyclerview with help of multi-viewholders

ex:

MainRecyclerview

-ChildRecyclerview1 (ViewHolder1)

-ChildRecyclerview2 (ViewHolder2)

-ChildRecyclerview3 (ViewHolder3) 

-Any other layout   (ViewHolder4)

Reference for multi-viewHolder - https://mcmap.net/q/48785/-how-to-create-recyclerview-with-multiple-view-types

Pastel answered 26/6, 2019 at 8:4 Comment(0)
C
3

Kotlin

Set isNestedScrollingEnabled to false for every RecyclerView that is under the scrolling view

val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.isNestedScrollingEnabled = false

Using XML Layout

<android.support.v7.widget.RecyclerView
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/friendsList"
    android:layout_width="match_parent"
    android:nestedScrollingEnabled="false"
    android:layout_height="wrap_content" />
Cumming answered 12/12, 2019 at 18:7 Comment(0)
P
2

If you are using VideoView or heavy weight widgets in your childviews keep your RecyclerView with height wrap_content inside a NestedScrollView with height match_parent Then scrolling will work smooth as perfectly as you want it.

FYI,

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

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:nestedScrollingEnabled="false"
            android:layout_height="wrap_content"
            android:clipToPadding="false" />

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

Thanks Micro this was from your hint!

karthik

Pocosin answered 13/7, 2018 at 6:37 Comment(1)
It is working for me too. But i do not understand how it exactly solve this issue ? What is the solution behind it ?Tresatrescha
S
1

you can use ScrollView as a parent and NestedScrollView as a child. like this:-

       <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/CL1">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/eventRV"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/CL1" />
    </androidx.core.widget.NestedScrollView>
Sematic answered 24/6, 2022 at 4:55 Comment(1)
NestedScrollView is just like ScrollView, but in NestedScrollView we can put other scrolling views as child of it, e.g. RecyclerView. But if we put RecyclerView inside NestedScrollView, RecyclerView's smooth scrolling is disturbed.Argil
M
0

XML code:

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

            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false" />

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

in java code :

  recycleView = (RecyclerView) findViewById(R.id.recycleView);
     recycleView.setNestedScrollingEnabled(false);
Maury answered 22/10, 2018 at 9:16 Comment(0)
D
0

Or you can just set android:focusableInTouchMode="true" in your recycler view

Dell answered 28/10, 2018 at 5:59 Comment(0)
O
0
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent">

            <android.support.constraint.ConstraintLayout
                android:id="@+id/constraintlayout_main"
                android:layout_width="match_parent"
                android:layout_height="@dimen/layout_width_height_fortyfive"
                android:layout_marginLeft="@dimen/padding_margin_sixteen"
                android:layout_marginRight="@dimen/padding_margin_sixteen"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent">

                <TextView
                    android:id="@+id/textview_settings"
                    style="@style/textviewHeaderMain"
                    android:gravity="start"
                    android:text="@string/app_name"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent" />

            </android.support.constraint.ConstraintLayout>

            <android.support.constraint.ConstraintLayout
                android:id="@+id/constraintlayout_recyclerview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/padding_margin_zero"
                android:layout_marginTop="@dimen/padding_margin_zero"
                android:layout_marginEnd="@dimen/padding_margin_zero"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/constraintlayout_main">

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/recyclerview_list"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:nestedScrollingEnabled="false"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent" />

            </android.support.constraint.ConstraintLayout>

        </android.support.constraint.ConstraintLayout>

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

</android.support.constraint.ConstraintLayout>

This code is working for in ConstraintLayout android

Osman answered 21/12, 2018 at 6:14 Comment(0)
R
0

Simple Add this line into your JAVA class

list.setNestedScrollingEnabled(false);
Reconcilable answered 16/12, 2020 at 20:17 Comment(0)
G
0

I myself had this issue, there is a recycler view within a scrollview and the scroll doesn't seem to be smooth. The cause for my issue was having the scrollview on top of the recycler view which was not needed for my requirement. So after I removed the scrollview and added the android:scrollbars="vertical" for recycler view, the scrolling was smooth.

Grunter answered 14/3, 2021 at 6:43 Comment(0)
V
0

After 3 days of research, I solved smooth scroll issue in my project.

The problem is <layer-list> drawable set in the background of item_user.xml file so it takes GPU time for rendering that's why scrolling not smooth. So please dont use complex <layer-list> drawable in the background of adapter item.

My problem is solved by above solution, below option is not useful for me

  1. setNestedScrollingEnabled
  2. setHasFixedSize
  3. setItemViewCacheSize
Vannessavanni answered 9/7, 2021 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.