我的nestedscrollview中有recyclerview,当我加载recyclerview时,滚动没有问题,它非常平滑,但一开始加载非常慢(与数据或网络无关),慢行是recyclerview.adapter = MyAdapter()
< / p>
当我将recyclerview移出nestedscrollview(root元素的第一个子元素)时,它的运行速度非常快。这是我的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)
{
}
}
}
答案 0 :(得分:0)
这是一个常见问题,在嵌套滚动视图中使用回收器视图时出现的问题会导致未定义回收器视图高度,因此,如果要在此回收器视图中显示1000个项目(嵌套滚动视图中的回收器视图),回收站适配器中的onCreateViewHolder()方法将被调用1000次,因为该回收站的高度尚未确定。
因此,要克服此问题,您必须使用
recyclerview.setNestedScrollingEnabled(false);
这将解决您的问题
此外,最好将回收器视图放置在嵌套滚动视图中时为其定义一个特定高度。
答案 1 :(得分:0)
当您有大量项目时,包含componentDidMount
将不支持平滑滚动。相反,将布局行为添加到componentDidUpdate
可以最大程度地减少滞后:
mRecyclerView.setNestedScrollingEnabled(false);
答案 2 :(得分:0)
我使用coordinatorlayout/collapsingtoolbar
解决了这个问题。