Java Android RecyclerView没有';不要回收物品
我有一个Java Android RecyclerView没有';不要回收物品,java,android,android-recyclerview,Java,Android,Android Recyclerview,我有一个recyclerView,它有多个recyclerView,具有动态膨胀,响应I am从服务器获取的是一个包含对象的列表,每个对象都包含一个列表和对象的属性 回收视图类型(垂直或水平)线性布局 除了定向是垂直的,适配器等待一次加载所有视图(停止循环)之外,一切正常 我找了很多东西想找到一个解决办法,但什么也找不到 主RecyclerView内部HomeFragment onBindViewHolder主页RecyclerView。 由于您的内部RV的高度设置为包裹内容物,因此无法为该箱子
recyclerView
,它有多个recyclerView
,具有动态膨胀,响应I
am从服务器获取的是一个包含对象的列表,每个对象都包含一个列表和对象的属性
回收视图
类型(垂直
或水平
)线性布局
除了定向是垂直的,适配器等待一次加载所有视图(停止循环)之外,一切正常
我找了很多东西想找到一个解决办法,但什么也找不到
主RecyclerView
内部HomeFragment
onBindViewHolder
主页RecyclerView
。
由于您的内部
RV
的高度设置为包裹内容物
,因此无法为该箱子启用回收。当它水平时,循环工作,因为它的宽度
是匹配父项
并且屏幕宽度是固定的。只有当有规定的高度时,回收才有效
但是,正如我所说,还有优化的余地:
现在,您正在为父RV的onBindViewHolder()
方法中的InternalRV
创建适配器,从而在每次父RVRV
决定通过重新绑定新值来重用其先前的Viewholder
时,都会强制InternalRV
充气Viewholder
您可以为父级RV
的内部onCreateVoewHolder()
创建适配器,并将新列表提交到内部适配器(随后调用notifyDatasetChanged()
)中OnBindViewHolder()
上述方法肯定会提高性能,至少在随后的滚动中是如此:回收将在父级RV
滚动上起作用,这意味着内部RV
将保留其视图持有者
,并在一定程度上重用它们
不幸的是,我没有时间为您的场景发布片段,但您可以看看我的另一个答案:
这是一个复杂的问题,但长话短说,其他滚动容器(如RecyclerView…)中的RecyclerView会导致整个过程停止回收。我在NestedScrollView或类似视图中放置recyclerView时观察到了这一点。这可能与“包裹内容”属性有关,导致RV膨胀所有视图以确定所需/可用空间,或者容器无法正确调整自身大小,除非RV报告其所需的“总”空间。。。如果你问我的话,那真是太差劲了:/lame?这正是
wrap_content
的意思,将wrap_content
添加到RV中没有意义,因为您只能在较小的容器中滚动较大的内容wrap_content使容器和内容更均匀。。。当内部RV已确定高度时,回收也起作用…没有解决方案。无法在嵌套在另一个recyclerview中的recyclerview上获取视图回收。有很多帖子证实了这一点。你要么接受它,要么重组你的代码,为一个recyclerview提供一个简单的项目列表。实际上,有太多的优化空间……在销售代表历史记录中,它表明你不接受答案(或尝试过)。我想知道答案是否有问题,还是只是点击错误?
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gt.gcook.ui.home.HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
android:nestedScrollingEnabled="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/home_single_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/seeAllTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginEnd="24dp"
android:text="See all"
android:textColor="@color/color_yellow"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/titleTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="Catigories"
android:textColor="#707070"
android:textSize="22sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:nestedScrollingEnabled="false"
app:reverseLayout="false"
android:clipToPadding="false"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/titleTV" />
</androidx.constraintlayout.widget.ConstraintLayout>
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
when (position) {
0 -> {
holder.itemView.titleTV.text = "Categories"
val mLayoutManager = LinearLayoutManager(holder.itemView.context)
mLayoutManager.orientation = RecyclerView.HORIZONTAL
holder.itemView.recyclerView.layoutManager = mLayoutManager
holder.itemView.recyclerView.adapter = HomeCategoryAdapter()
}
1 -> { //here is the problem when Orintation is vertical.
holder.itemView.titleTV.text = "Most rated recipes"
val mLayoutManager = GridLayoutManager(holder.itemView.context, 2)
mLayoutManager.orientation = RecyclerView.VERTICAL
holder.itemView.recyclerView.layoutManager = mLayoutManager
holder.itemView.recyclerView.adapter = MostRatedRecipeAdapter(clickListener)
}
else -> {
holder.itemView.titleTV.text = "Favourite"
val mLayoutManager = LinearLayoutManager(holder.itemView.context)
mLayoutManager.orientation = RecyclerView.HORIZONTAL
holder.itemView.recyclerView.layoutManager = mLayoutManager
holder.itemView.recyclerView.adapter = HomeCategoryAdapter()
}
}
}