Ich habe recyclerview in meiner nestedscrollview. Wenn mein recyclerview geladen wird, habe ich kein Problem mit dem Scrollen. Es ist sehr flüssig, aber das Laden ist zunächst sehr langsam (es geht nicht um meine Daten oder das Netzwerk). Die langsame Zeile ist recyclerview.adapter = MyAdapter()

Wenn ich die Recycling-Ansicht aus nestedscrollview (erstes untergeordnetes Element des Stammelements) verschiebe, ist es sehr schnell. Hier ist meine XML

Mit dieser XML-Datei wird die Benutzeroberfläche für 3-4 Sekunden blockiert und geladen. Wenn ich jedoch meine Recycling-Ansicht außerhalb von nestedscrollview platziere, wird sie in Millisekunden geladen und nicht bemerkt.

Ich benötige die Recycling-Ansicht in meiner 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 für 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

1
Emre Akcan 3 Juli 2019 im 12:29

3 Antworten

Beste Antwort

Ich habe das Problem mit coordinatorlayout/collapsingtoolbar gelöst.

1
Tamir Abutbul 3 Juli 2019 im 17:53

Dies ist ein häufiges Problem. Das Problem bei der Verwendung der Recycler-Ansicht in einer verschachtelten Bildlaufansicht führt dazu, dass die Höhe der Recycler-Ansicht nicht definiert ist. Wenn in dieser Recycler-Ansicht 1000 Elemente angezeigt werden sollen (Recycler-Ansicht in der verschachtelten Bildlaufansicht), wird onCreateViewHolder () Die Methode im Recycler-Adapter wird 1000-mal aufgerufen, da die Höhe dieses Recyclers nicht bestimmt wird.
Um dieses Problem zu beheben, müssen Sie sie verwenden

recyclerview.setNestedScrollingEnabled(false);

Dadurch wird Ihr Problem behoben.
Außerdem ist es besser, eine bestimmte Höhe für die Recycler-Ansicht zu definieren, wenn diese in einer verschachtelten Bildlaufansicht platziert wird.

0
Shalan93 3 Juli 2019 im 09:50

Das Einschließen von mRecyclerView.setNestedScrollingEnabled(false); unterstützt kein reibungsloses Scrollen, wenn Sie eine erhebliche Anzahl von Elementen haben. Fügen Sie stattdessen Ihrem RecyclerView ein Layoutverhalten hinzu, um den größten Teil der Verzögerung zu minimieren:

 <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/contact_recycler"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    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"/>
0
SaadAAkash 3 Juli 2019 im 09:55