Home > Software engineering >  Using Recyclerviews inside nestedscrollview is causing speed issue
Using Recyclerviews inside nestedscrollview is causing speed issue

Time:12-25

I am using 2 recyclerviews inside nestedscrollview, but after items loaded, app is very slow. it's not scrolling smooth I tried to optimize images and views, but it didn't help at all.

<androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@color/md_grey_100"
            android:clickable="true"
            android:fillViewport="true"
            android:focusable="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@id/layout_toolbar">

            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                      <androidx.recyclerview.widget.RecyclerView
                    android:id="@ id/rv_service_types"
                    android:layout_width="match_parent"
                    android:layout_height="42dp"
                    android:layout_marginTop="8dp"
                    android:nestedScrollingEnabled="false" />
                    
                          <androidx.recyclerview.widget.RecyclerView
                    android:id="@ id/rv_stores"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="8dp"
                    android:nestedScrollingEnabled="false" />

    </androidx.appcompat.widget.LinearLayoutCompat>
    
     </androidx.core.widget.NestedScrollView>

CodePudding user response:

You should avoid using RecyclerView inside NestedScrollView if you have big number of items in RecyclerView If it's items consist images from online, it's causing much laggings and delays Instead using different RecyclerViews, try to use one. In this case, you should define different viewtypes for Adapter.

class YourAdapter(){
override fun getItemViewType(position: Int): Int {
    return when (position) {
       x-> xx
       y-> yy
    }
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
    return when (viewType) {
        xx-> YourFirstViewHolder
        yy ->YourSecondViewHolder
    }
}
  • Related