Recycler View loading very slow for large data when inside NestedScrollView
Asked Answered
C

5

14

I have added RecyclerView inside my NestedScrollView. Basically I want RecyclerView to scroll with other Views. The problem that I am facing is that for a small set of data, it is working fine, but for a large set of data(200 entries) whenever I launch the activity, it freezes for about about 3-5 seconds and then loads. I removed the NestedScrollView and it is working flawlessly, but it doesn't provide me the behaviour I want.

(For extra info, I am loading the data from SQLite database. There is no problem in scrolling, as it is smooth. The only problem is the activity is freezing for a while)

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

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

                <... Some other Views ...>

                <android.support.v7.widget.RecyclerView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                </android.support.v7.widget.RecyclerView>

            </LinearLayout>

        </android.support.v4.widget.NestedScrollView>
Courtenay answered 16/6, 2017 at 4:36 Comment(6)
try recyclerView.setNestedScrollingEnabled(false);Brooking
yes I have tried it, its not working, as I said, there is no problem with the scrollingCourtenay
Please show your java code, where you are fetching data from database and you are populating that data into your layout.Middling
The database query is being executed on a new IO thread, and the data is being passed to the main thread, then the list is being passed to the recycler adapter, as I said there is no problem when I remove the nested scroll view!Courtenay
@Gurleen Sethi Have you found any solution ?Haystack
The problem is when you inflate recycler view inside a nested scroll view, it start inflating a view for every item, so if you have 500 items in your recycler adapter then it will create 500 item layout which causes the sudden unresponsiveness, what I did is removed the nested scroll layout, haven't found any solution that can make nested scroll view work with recycler view in case of large amount of data.Courtenay
S
19

This case of RecyclerView inside NestedScrollView.

RecyclerView is calling onCreateViewHolder() times equal to your data size.

If data has 200 items, it freezes for onCreateViewHolder() to be called 200 times.

Sperling answered 16/6, 2017 at 5:14 Comment(8)
because root view NestedScrollView, recyclerview height is infinityHards
So how can we overcome this problem?Courtenay
? find another way. use github.com/martijnvdwoude/recycler-view-merge-adapterHards
dont RecyclerView in NestedScrollViewHards
or <android.support.v7.widget.RecyclerView android:layout_width="wrap_content" android:layout_height="500dp" android:orientation="vertical">Hards
recyclerview height fixHards
@Sperling When I try that fix -- android:layout_height="500dp" -- with recycler.setNestedScrollingEnabled(false); then what happens is that the recyclerview never scrolls to show more data than what appears initially. But if I remove that method call it works right - it loads the data as a recyclerview should - bit by bit as needed. Scrolling may not be as smooth but in landscape mode my recyclerview is not even visible so I need whole fragment to scroll up.Kassey
@배준모, Thanks dude, it saves lot of my time!Suggestion
A
0

The problem as said above is because RecyclerView as a child or subChild in NestedScrollView measures its height as infinitive when you use WRAP_CONTENT or MATCH_PARENT for height of RecyclerView.

one solution that solved this problem for me was setting the RecyclerView Height to a fixed size. you could set height to a dp value, or you could set it to a pixel value matching devices height if your requirements needs a vertical infinitive RecyclerView.

here is a snippet for setting the recyclerView size in kotlin

    val params = recyclerView.layoutParams
    params.apply {
            width = context.resources.displayMetrics.widthPixels
            height = context.resources.displayMetrics.heightPixels
    }
    recyclerView.layoutParams = params
Annabelleannabergite answered 29/3, 2020 at 13:51 Comment(0)
C
0

I faced the same problem! The solution was to change NestedScrollView to SwipeRefreshLayout.

add this for enable/disable ToolBar Scrolling:

ViewCompat.setNestedScrollingEnabled(recyclerView, true);

Confectioner answered 15/9, 2021 at 23:10 Comment(0)
T
0

Yes, the answer is a bit late, but I think this will prevent you from freezing your RecyclerView inside any kind of Scrollable Views like another RecyclerView, ScrollView, NestedScrollView, MotionView, etc...

All you have to do is give your RecyclerView a fixed height.

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_home"
    android:layout_width="match_parent"
    android:layout_height="192dp"/>
Tletski answered 25/8, 2023 at 1:59 Comment(0)
N
-5

As said by Nancy , recyclerview.setNestedScrollingEnabled(false); will solve scroll stuck issue. i too faced this type of issue and solved by false the NestedScroll.

Natheless answered 16/6, 2017 at 5:17 Comment(2)
Scrolling is not the issue, the issue is that activity freezes for a couple of seconds, when it loads completely it scrolls flawlessly, I use the setNestedScrollingEnabled(false) options always, but no win with that!Courtenay
@GurleenSethi have you found any solution for this yet? I am facing the same problem now.Paulenepauletta

© 2022 - 2024 — McMap. All rights reserved.