简体   繁体   中英

Recycler View loading very slow for large data when inside NestedScrollView

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>

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.

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

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

add this for enable/disable ToolBar Scrolling:

ViewCompat.setNestedScrollingEnabled(recyclerView, true);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM