嵌套滚动视图中的回收视图加载缓慢
recyclerview inside nestedscrollview loading slow
我的 nestedscrollview 中有 recyclerview,当我的 recyclerview 加载时,我的滚动没有问题,它非常流畅但一开始加载非常慢(这与我的数据或网络无关)慢行是 recyclerview.adapter = MyAdapter()
当我将 recyclerview 移出 nestedscrollview(根元素的第一个子元素)时,速度非常快。这是我的 xml
使用此 xml 它会阻止 ui 3-4 秒并加载它,但如果我将我的 recyclerview 放在 nestedscrollview 之外,它会在几毫秒内加载,不会引起注意。
我的 nestedscrollview 中需要 recyclerview。
<?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>
适配器代码
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)
{
}
}
}
这是一个常见问题,在嵌套滚动视图中使用回收视图时会导致回收视图高度未定义,因此如果要在此回收视图中显示 1000 个项目(嵌套滚动视图中的回收视图) recycler 适配器中的 onCreateViewHolder() 方法将被调用 1000 次,因为这个 recycler 的高度未确定。
所以要克服这个问题你必须使用
recyclerview.setNestedScrollingEnabled(false);
这将解决您的问题
此外,当它被放置在嵌套滚动视图中时,最好为回收视图定义一个特定的高度。
我使用 coordinatorlayout/collapsingtoolbar
解决了这个问题。
我的 nestedscrollview 中有 recyclerview,当我的 recyclerview 加载时,我的滚动没有问题,它非常流畅但一开始加载非常慢(这与我的数据或网络无关)慢行是 recyclerview.adapter = MyAdapter()
当我将 recyclerview 移出 nestedscrollview(根元素的第一个子元素)时,速度非常快。这是我的 xml
使用此 xml 它会阻止 ui 3-4 秒并加载它,但如果我将我的 recyclerview 放在 nestedscrollview 之外,它会在几毫秒内加载,不会引起注意。
我的 nestedscrollview 中需要 recyclerview。
<?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>
适配器代码
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)
{
}
}
}
这是一个常见问题,在嵌套滚动视图中使用回收视图时会导致回收视图高度未定义,因此如果要在此回收视图中显示 1000 个项目(嵌套滚动视图中的回收视图) recycler 适配器中的 onCreateViewHolder() 方法将被调用 1000 次,因为这个 recycler 的高度未确定。
所以要克服这个问题你必须使用
recyclerview.setNestedScrollingEnabled(false);
这将解决您的问题
此外,当它被放置在嵌套滚动视图中时,最好为回收视图定义一个特定的高度。
我使用 coordinatorlayout/collapsingtoolbar
解决了这个问题。