13

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>
Narendra Singh
  • 4,513
  • 5
  • 30
  • 75
Gurleen Sethi
  • 2,299
  • 4
  • 18
  • 39
  • try `recyclerView.setNestedScrollingEnabled(false);` – Jaydeep Devda Jun 16 '17 at 04:41
  • yes I have tried it, its not working, as I said, there is no problem with the scrolling – Gurleen Sethi Jun 16 '17 at 04:48
  • Please show your java code, where you are fetching data from database and you are populating that data into your layout. – singh.indolia Jun 16 '17 at 04:49
  • 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! – Gurleen Sethi Jun 16 '17 at 04:54
  • @Gurleen Sethi Have you found any solution ? – Zohaib Akram Jul 06 '17 at 07:02
  • 5
    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. – Gurleen Sethi Jul 08 '17 at 06:07

3 Answers3

17

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.

Gurleen Sethi
  • 2,299
  • 4
  • 18
  • 39
배준모
  • 561
  • 5
  • 11
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
ilaimihar
  • 101
  • 5
-4

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.

Darsy
  • 115
  • 1
  • 6
  • 4
    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! – Gurleen Sethi Jun 16 '17 at 05:59
  • 1
    @GurleenSethi have you found any solution for this yet? I am facing the same problem now. – viper Aug 08 '17 at 04:53