'Kotlin: issue with NestedScrollView with RecyclerView loading all data at start

I have a fragment that has multiple elements in it, some textViews, imageViews, a page ViewPager for ads and at the bottom I have a recyclerView. I want them all to scroll as one so I places then in a nested scrollView. I set the recyclerView height to wrap_content and set the recyclerView.isNestedScrollingEnabled = false. Everything works fine with small dataset but when I have a large dataset for the recyclerView it takes a few second and freeze the UI. I see that the whole dataset is being is being drawn at the start and its not behaving like a recyclerView that only draws elements on the screen.(I know why this happen, because the recyclerView is as tall as the dataset, so please don't tell me this.). I know that having a scrolling widget inside a scrolling widget is not best practice but its the only way I have got the correct behavior to work. Is there a way to have a recyclerView in a nested scroll view that only loads the part of the recyclerView but still scrolls as one? Or is there a better way to structure this layout?

(Is it posible to have the

binding.RecyclerView.adapter = RecyclerView_Adapter()

on a different/background thread? I don't think so because its doing UI operations)

(I have tried setting a specific height to the recycler view but it scrolls independently from the Nested scrollView )

XML

 <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/inicio_swipe_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/NestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:minHeight="300dp"
                app:layout_constraintBottom_toTopOf="@+id/"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/">

                <TextView
                    android:id="@+id/"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginEnd="8dp"
                    android:text=""
                    android:textColor="@color/colorPrimaryDark"
                    android:textStyle="bold"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <androidx.viewpager.widget.ViewPager
                    android:id="@+id/"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="4dp"
                    android:minHeight="200dp"
                    app:layout_constraintBottom_toTopOf="@+id/"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/" />

                <LinearLayout
                    android:id="@+id/"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="8dp"
                    android:gravity="center_vertical|center_horizontal"
                    android:orientation="horizontal"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"></LinearLayout>

                <ProgressBar
                    android:id="@+id/"
                    style="?android:attr/progressBarStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:minHeight="200dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/ConstraintLayout_Inicio_Avisos">

                <TextView
                    android:id="@+id/"
                    android:layout_width="0dp"
                    android:layout_height="1dp"
                    android:background="#c6c2c2"
                    android:text="TextView"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />


                <ImageView
                    android:id="@+id/"
                    android:layout_width="0dp"
                    android:layout_height="75dp"
                    android:layout_marginStart="24dp"
                    android:layout_marginTop="4dp"
                    android:layout_marginEnd="24dp"
                    android:src="@drawable/centralimperdibles"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/line1" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="24dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/" />

                <ProgressBar
                    android:id="@+id/"
                    style="?android:attr/progressBarStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>



        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

fragment

val Grid = GridLayoutManager(activity, 2, RecyclerView.VERTICAL,false) binding.RecyclerView.layoutManager = Grid binding.RecyclerView.setHasFixedSize(true) binding.RecyclerView.isNestedScrollingEnabled = false binding.RecyclerView.adapter = RecyclerView_Adapter()

The ideal behavior is that as you scroll down the top widgets disappear until the recycler view is the whole screen height and then the recyclerView can scroll.

Thank you.

EDIT

Trying to pagina the recycler view but the update not working. If I reset the adapter(the section commented out) it works but its slow because it drawing 40 items. Why is notifyItemRangeInserted not working?

binding.NestedScrollView.setOnScrollChangeListener(androidx.core.widget.NestedScrollView.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->

        if (scrollY >= binding.ConstraintLayoutInicio.measuredHeight - v.measuredHeight - 1000) {
            if(bottomOfScroll == 0) {
                println("BOTTOM SCROLL")

                if (ProductosForListaParts.value!![0].Id != "0") {
                    val newcount = ProductosForListaParts.value!!.count() + 20
                    var productosListPartsToAdd: ArrayList<Producto> = ArrayList()
                    ProductosForLista.value?.forEachIndexed { i, producto ->
                        if (i >= newcount-20 && i < newcount) {
                            productosListPartsToAdd.add(producto)
                        }
                    }
                    ProductosForListaParts.value!!.addAll(productosListPartsToAdd)
                    
                    binding.RecyclerViewHotBuy.adapter?.notifyItemRangeInserted(newcount - 20, 20)
                    
                    //binding.RecyclerViewHotBuy.adapter = Inicio_Producto_Adapter(
                    //    ProductosForListaParts.value!!, OrganizationDollar.value!!, SubirAlCarritoProductos,
                    //    CarritoProductosArray, BajarDelCarritoProductos, event, eventPesables)
                }

            }
        }
    })


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source