193

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" /> 
3
  • this solution work for me: stackoverflow.com/a/32390370/7308789 thank you Commented Mar 19, 2017 at 19:44
  • 1
    @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? Commented Jun 21, 2019 at 5:51
  • Use androidx.constraintlayout.widget.ConstraintLayout which will solve your problem without any complex implementations Commented Jun 26, 2019 at 13:19

19 Answers 19

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 > 
Sign up to request clarification or add additional context in comments.

14 Comments

To add to this answer: setNestedScrollingEnabled(false) only worked when I switched out the ScrollView for a NestedScrollView instead.
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?
Note that android:nestedScrollingEnabled="false" only works for API 21+ but v.setNestedScrollingEnabled(false) is OK for <21.
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 stackoverflow.com/a/38995399/132121
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 recycler
|
88

I only needed to use this:

mMyRecyclerView.setNestedScrollingEnabled(false); 

in my onCreateView() method.

Thanks a lot!

Comments

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 .

1 Comment

requires Api 21+
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); 

Comments

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> 

Comments

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.

Comments

5

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

Comments

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); 

Comments

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://stackoverflow.com/a/33143512/3879847

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://stackoverflow.com/a/33143512/3879847

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://stackoverflow.com/a/26245463/3879847

Comments

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" /> 

Comments

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

1 Comment

It is working for me too. But i do not understand how it exactly solve this issue ? What is the solution behind it ?
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> 

1 Comment

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.
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); 

Comments

0

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

Comments

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

Comments

0

Simple Add this line into your JAVA class

list.setNestedScrollingEnabled(false); 

Comments

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.

Comments

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

Comments

0
<androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/activity_e_store_ll_bottom" android:layout_below="@+id/activity_e_store_appBarLayout"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="150dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginRight="10dp" android:background="@color/colorWhite" app:cardCornerRadius="10dp" app:cardPreventCornerOverlap="false" app:cardUseCompatPadding="true"> <FrameLayout android:layout_width="match_parent" android:layout_height="150dp" android:background="@color/colorWhite"> <androidx.viewpager.widget.ViewPager android:id="@+id/activity_e_store_view_pager" android:layout_width="match_parent" android:layout_height="200dp" /> <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="gone" /> <com.rd.PageIndicatorView android:id="@+id/activity_e_store_indicator_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" android:paddingBottom="10dp" app:piv_animationType="drop" app:piv_dynamicCount="true" app:piv_interactiveAnimation="true" app:piv_radius="3dp" app:piv_selectedColor="@color/colorPrimary" app:piv_unselectedColor="@color/colorWhite" app:piv_viewPager="@+id/container_shop_view_pager" /> </FrameLayout> </androidx.cardview.widget.CardView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="20dp" android:layout_marginRight="10dp" android:orientation="horizontal"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="0.2" android:text="SHOP BY CATEGORY" android:textColor="@color/colorBlack" android:textSize="14sp" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.8" android:background="@drawable/bg_play" android:gravity="center" android:paddingLeft="8dp" android:paddingTop="5dp" android:paddingRight="8dp" android:paddingBottom="5dp" android:text="View All" android:textColor="@color/colorWhite" android:textSize="12sp" android:visibility="invisible" /> </LinearLayout> <androidx.recyclerview.widget.RecyclerView android:id="@+id/activity_e_store_recyclerView_shop_category" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginRight="10dp" /> <!--Treatment Disease wise package--> <TextView android:id="@+id/activity_e_store_textView_treatment_package" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@drawable/bg_modules" android:elevation="8dp" android:gravity="center" android:padding="10dp" android:text="My package" android:textColor="@color/colorPrimaryDark" android:textSize="14sp" /> <androidx.recyclerview.widget.RecyclerView android:id="@+id/activity_e_store_recyclerView_home_products" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" /> </LinearLayout> </androidx.core.widget.NestedScrollView> 

1 Comment

try adding nestedscroll to false in recyclerview, it will work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.