2

I have recyclerview inside my nestedscrollview, when my recyclerview loads, I have no problem with scrolling, its very smooth but its loading very slow at first (its not about my data or networking) slow line is recyclerview.adapter = MyAdapter()

when I move the recyclerview out of nestedscrollview (first child of root element) its really fast. here is my xml

with this xml it blocks the ui for 3-4 seconds and loads it but if I place my recyclerview outside of nestedscrollview it loads in milliseconds, unnoticable.

I need the recyclerview in my nestedscrollview.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:tools="http://schemas.android.com/tools" 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusableInTouchMode="true"
        android:background="#f7f7f7"
        android:orientation="vertical">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:titleTextAppearance="@style/ToolbarExpandedTextAppearance"
            app:layout_collapseMode="pin"
            android:background="@color/colorPrimary">
            <ImageButton
                android:layout_width="?attr/actionBarSize"
                android:layout_height="?attr/actionBarSize"
                android:id="@+id/right_button"
                android:src="@drawable/ic_top_question"
                android:layout_gravity="end|top"
                android:contentDescription="@string/placeholder_right_button"
                android:background="?attr/selectableItemBackground"/>
        </androidx.appcompat.widget.Toolbar>

        <androidx.core.widget.NestedScrollView
            android:id="@+id/nestedScrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:orientation="vertical">

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

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimary" android:id="@+id/constraintLayout">

                    <TextView
                        android:text="Give £8, Get £8"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/white"
                        android:textSize="@dimen/field_header_title_text"
                        android:fontFamily="@font/apercu_medium"
                        android:id="@+id/textView2" android:layout_marginTop="8dp"
                        android:layout_marginStart="16dp"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"/>
                    <TextView
                        android:text="Give your friends £8 with your code and get £8 for every friend that uses it!"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/apercu"
                        android:textSize="@dimen/discover_message_text_size"
                        android:textColor="@color/text_white"
                        android:lineSpacingExtra="10dp"
                        android:id="@+id/textView3" android:layout_marginTop="16dp"
                        app:layout_constraintTop_toBottomOf="@+id/textView2"
                        app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
                        android:layout_marginEnd="32dp" android:layout_marginStart="16dp"/>
                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="0dp"
                        android:layout_height="80dp"
                        app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
                        app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
                        android:layout_marginTop="24dp" app:layout_constraintTop_toBottomOf="@+id/textView3"
                        android:id="@+id/linearLayout" android:layout_marginBottom="8dp"
                        app:layout_constraintBottom_toBottomOf="parent">
                        <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="0dp"
                            android:layout_weight="1"
                            android:layout_height="match_parent"
                            android:gravity="center"
                            android:layout_margin="8dp"
                            android:background="@drawable/white_radius">
                            <TextView
                                android:layout_width="match_parent"
                                android:fontFamily="@font/apercu_bold"
                                android:textColor="@color/warm_grey"
                                android:layout_height="wrap_content"
                                android:gravity="center"
                                android:textSize="24sp"
                                android:id="@+id/txt_pending_invites"/>
                            <TextView
                                android:text="Pending invites"
                                android:layout_width="match_parent"
                                android:fontFamily="@font/apercu"
                                android:lineSpacingExtra="10dp"
                                android:textColor="@color/greyish"
                                android:layout_height="wrap_content" android:id="@+id/textsView5"
                                android:gravity="center"/>
                        </LinearLayout>

                        <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="0dp"
                            android:layout_weight="1"
                            android:gravity="center"
                            android:layout_margin="8dp"
                            android:layout_height="match_parent"
                            android:background="@drawable/white_radius">
                            <TextView
                                android:layout_width="match_parent"
                                android:fontFamily="@font/apercu_bold"
                                android:textColor="@color/warm_grey"
                                android:layout_height="wrap_content"
                                android:gravity="center"
                                android:textSize="24sp"
                                android:id="@+id/txt_earned"/>
                            <TextView
                                android:text="Total earned to date"
                                android:layout_width="match_parent"
                                android:fontFamily="@font/apercu"
                                android:lineSpacingExtra="10dp"
                                android:textColor="@color/greyish"
                                android:layout_height="wrap_content" android:id="@+id/textView5"
                                android:gravity="center"/>
                        </LinearLayout>

                    </LinearLayout>

                </androidx.constraintlayout.widget.ConstraintLayout>

                <androidx.constraintlayout.widget.ConstraintLayout
                        android:layout_width="match_parent"
                        android:layout_height="100dp"
                        tools:layout_editor_absoluteX="16dp" android:layout_marginTop="8dp"
                        app:layout_constraintTop_toBottomOf="@+id/constraintLayout"
                        android:id="@+id/constraintLayout3">

                    <androidx.constraintlayout.widget.ConstraintLayout
                            android:layout_width="0dp"
                            android:background="@drawable/edittextround"
                            android:layout_height="50dp" app:layout_constraintBottom_toBottomOf="parent"
                            android:layout_marginBottom="8dp" app:layout_constraintStart_toStartOf="parent"
                            android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"
                            app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="15dp"
                            android:layout_marginEnd="15dp" android:id="@+id/constraintLayout2">
                        <TextView
                            android:id="@+id/txt_promo_code"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:maxLines="1"
                            android:textSize="16sp"
                            android:text=""
                            android:textColor="@color/evergreen"
                            android:background="@android:color/transparent" android:layout_marginBottom="8dp"
                            app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="8dp"
                            app:layout_constraintTop_toTopOf="parent"
                            app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="20dp"
                            app:layout_constraintEnd_toStartOf="@+id/invite_button"/>
                        <Button
                            android:text="Share"
                            android:layout_width="wrap_content"
                            android:background="@drawable/form_button_radius"
                            android:textAppearance="@style/BottomButtonAppearance"
                            android:layout_height="32dp"
                            android:id="@+id/invite_button"
                            app:layout_constraintEnd_toEndOf="parent"
                            android:layout_marginEnd="8dp" android:layout_marginBottom="0dp"
                            app:layout_constraintBottom_toBottomOf="parent" android:layout_marginTop="0dp"
                            app:layout_constraintTop_toTopOf="parent"/>

                    </androidx.constraintlayout.widget.ConstraintLayout>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="?android:colorBackground"
                        android:includeFontPadding="false"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:textSize="13sp"
                        android:text="Share Code"
                        android:textColor="@color/warm_grey"
                        android:fontFamily="@font/apercu"
                        android:textAppearance="@android:style/TextAppearance.Medium"
                        app:layout_constraintEnd_toEndOf="@+id/constraintLayout2"
                        app:layout_constraintStart_toStartOf="@+id/constraintLayout2"
                        app:layout_constraintHorizontal_bias="0.05" android:layout_marginTop="8dp"
                        app:layout_constraintTop_toTopOf="@+id/constraintLayout2"
                        app:layout_constraintBottom_toTopOf="@+id/constraintLayout2"/>

                </androidx.constraintlayout.widget.ConstraintLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18sp"
                    android:textColor="@color/black"
                    android:letterSpacing="-0.01"
                    android:lineSpacingExtra="4sp"
                    android:text="Invite your contacts"
                    app:layout_constraintTop_toBottomOf="@+id/constraintLayout3"
                    android:layout_marginStart="16dp" app:layout_constraintStart_toStartOf="parent"
                    android:id="@+id/textView" android:layout_marginTop="12dp"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="16sp"
                    android:textColor="@color/warm_grey"
                    android:lineSpacingExtra="10sp"
                    android:visibility="gone"
                    android:text="Send your friends and family a link and you both get rewarded when they sign up."
                    android:layout_marginTop="12dp"
                    app:layout_constraintTop_toBottomOf="@+id/textView" android:id="@+id/txt_invite"
                    android:layout_marginStart="16dp" app:layout_constraintStart_toStartOf="parent"/>

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="220dp"
                    android:background="@drawable/white_radius"
                    app:layout_constraintTop_toBottomOf="@+id/txt_invite"
                    app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
                    app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
                    android:id="@+id/permission_layout">
                    <Button
                        android:text="Give Access"
                        android:layout_width="0dp"
                        android:background="@drawable/form_button_radius"
                        android:textAppearance="@style/BottomButtonAppearance"
                        android:layout_height="48dp"
                        android:id="@+id/btn_give_access"
                        app:layout_constraintEnd_toEndOf="parent"
                        android:layout_marginEnd="16dp"
                        android:layout_marginStart="16dp"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/textView6" android:layout_marginTop="16dp"/>
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="13sp"
                        android:textColor="@color/warm_grey"
                        android:lineSpacingExtra="7sp"
                        android:gravity="center_horizontal"
                        android:text="@string/to_send_your_friends_and_family_a_referral_link_please_give_us_access_to_your_contacts"
                        tools:layout_editor_absoluteX="0dp"
                        android:layout_marginTop="16dp" app:layout_constraintTop_toBottomOf="@+id/imageView2"
                        android:id="@+id/textView6"/>
                    <ImageView
                        android:layout_width="60dp"
                        android:layout_height="60dp" app:srcCompat="@drawable/icon_person"
                        android:id="@+id/imageView2" android:layout_marginTop="20dp"
                        app:layout_constraintTop_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent"
                        android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="parent"
                        android:layout_marginStart="8dp"/>
                </androidx.constraintlayout.widget.ConstraintLayout>


                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/contact_recycler"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    tools:listitem="@layout/item_contact"
                    tools:layout_editor_absoluteX="0dp" android:layout_marginTop="16dp"
                    app:layout_constraintTop_toBottomOf="@+id/permission_layout"/>

                <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content"
                    app:layout_constraintTop_toBottomOf="@+id/permission_layout"
                    android:indeterminate="true"
                    android:indeterminateTintMode="src_atop"
                    android:indeterminateTint="@color/colorPrimary"
                    android:visibility="gone"
                    app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
                    app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="17dp"
                    android:progressTint="@color/warm_grey" android:layout_marginTop="24dp"
                    android:id="@+id/progress_bar"/>

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.core.widget.NestedScrollView>

    </LinearLayout>
<layout>

Code for adapter

internal class ContactAdapter(context: Context, private val mProductList:   ArrayList<ContactModel>) : RecyclerView.Adapter<ContactAdapter.MyViewHolder>() {
    private val inflater: LayoutInflater;
    private val key: String? = null;

    init {
        inflater = LayoutInflater.from(context);
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = inflater.inflate(R.layout.item_contact, parent, false);
        return MyViewHolder(view);
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int){
        val selectedProduct = mProductList[position];
        holder.setData(selectedProduct);

    }

    override fun getItemCount(): Int {
        return mProductList.size;
    }

    internal inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
        fun setData(selectedProduct: ContactModel)
        {
            itemView.txt_name.text = selectedProduct.name;
            itemView.txt_number.text = selectedProduct.number;
        }

        override fun onClick(v: View)
        {

        }
    }
}

screenshot for screen

10
  • This answer solves your problem. You must set nestedScrollingEnabled to false Commented Jul 3, 2019 at 9:34
  • 7
    No it doesn't work for me. Commented Jul 3, 2019 at 9:38
  • nestedScrollingEnabled=flase, you must use this although it is not working for you, post the code for adapter Commented Jul 3, 2019 at 9:59
  • @LakhwinderSingh its simple contact adapter, updated the code please look at it Commented Jul 3, 2019 at 10:02
  • The adapter is looking it has no issues in that Commented Jul 3, 2019 at 10:04

2 Answers 2

3

I solved the problem using coordinatorlayout/collapsingtoolbar.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please explain how? Am facing the same issue
@Srihari Sorry for late answer, like I said I've used collapsing toolbar, so I put the blue part in screenshot into my toolbar and disabled any animation like parallax
-1

This is a common problem, the problem when using recycler view inside a nested scroll view this cause the recycler view height not defined so if you have 1000 item to be displayed in this recycler view (recycler view inside nested scroll view) the onCreateViewHolder() method in recycler adapter will be called 1000 times because the hight of this recycler isn't determined.
So to overcome this issue you have to use

recyclerview.setNestedScrollingEnabled(false);

this will solve your issue
Also, it's better to define a certain height for recycler view when it is placed inside a nested scroll view.

1 Comment

so you have to remove it outside the nested scroll view or you have to define a fixed height for the recycler view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.