android - 子回收器视图的预取视图不起作用
问题描述
我有带有子(嵌套)水平 RecyclerView 的垂直 RecyclerView。对于第二个回收器,我使用布局管理器:
LinearLayoutManager(itemView.context, LinearLayoutManager.HORIZONTAL, false).apply {
isItemPrefetchEnabled = true
initialPrefetchItemCount = 4
}
但是嵌套回收器只构建前两个可见项目。
完整代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recycler = findViewById<RecyclerView>(R.id.recycler)
recycler.layoutManager = LinearLayoutManager(this)
recycler.adapter = MainAdapter(buildElements())
}
private fun buildElements(): List<Parent> {
return (0..2).map { pCount -> createParent(pCount) }
}
private fun createParent(index: Int): Parent {
return Parent(text = "$index",
list = (0..30).map { Child("item $it") }
)
}
}
主适配器
class MainAdapter(private val list: List<Parent>) : RecyclerView.Adapter<MainAdapter.CustomHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomHolder {
val inflater = LayoutInflater.from(parent.context)
return CustomHolder(inflater.inflate(R.layout.item, parent, false))
}
override fun getItemCount(): Int = list.size
override fun onBindViewHolder(holder: CustomHolder, position: Int) {
holder.bind(list[position])
}
class CustomHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val recycler = itemView.findViewById<RecyclerView>(R.id.recyclerView)
private val title = itemView.findViewById<TextView>(R.id.title)
init {
recycler.layoutManager =
LinearLayoutManager(itemView.context, LinearLayoutManager.HORIZONTAL, false).apply {
isItemPrefetchEnabled = true
initialPrefetchItemCount = 4
}
}
fun bind(data: Parent) {
title.text = data.text
recycler.adapter = ChildAdapter(data.list)
}
}
}
主要回收元件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="16dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>
子适配器
class ChildAdapter(private val list: List<Child>) : RecyclerView.Adapter<ChildAdapter.ChildHolder>() {
companion object {
private val TAG = ChildAdapter::class.java.simpleName
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChildHolder {
val inflater = LayoutInflater.from(parent.context)
return ChildHolder(inflater.inflate(R.layout.child, parent, false))
}
override fun getItemCount(): Int = list.size
override fun onBindViewHolder(holder: ChildHolder, position: Int) {
holder.bind(list[position])
}
class ChildHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val title = itemView.findViewById<TextView>(R.id.childText)
fun bind(data: Child) {
title.text = data.text
Log.d(TAG, "bind child element: ${data.text}")
}
}
}
和子元素:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="275dp"
android:padding="8dp"
android:layout_height="100dp">
<TextView
android:id="@+id/childText"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content"/>
</FrameLayout>
在日志中只看到这个:
D/ChildAdapter: bind child element: item 0
D/ChildAdapter: bind child element: item 1
D/ChildAdapter: bind child element: item 0
D/ChildAdapter: bind child element: item 1
D/ChildAdapter: bind child element: item 0
D/ChildAdapter: bind child element: item 1
日志意味着只构建了第一个和第二个元素,但我想要预构建第三个和第四个元素。
我也尝试使用它,但它也不起作用
解决方案
预取仅适用于滚动,不适用于第一个初始项目的创建。
最后,我使用带有 overridegetExtraLayoutSpace
和 return的自定义布局管理器100000
。我的解决方案有潜在危险,但我使用它,因为我知道项目数不能超过 10 个项目。
推荐阅读
- mvvmcross - 当有多个测试类时,“您应该只初始化 MvxBindingSingletonCache 一次”错误
- powershell - 如何将对象数组拆分为数组中的单独变量
- dart - 如何将函数字节转换为飞镖
- r - 避免R中for循环的巧妙方法
- swift - AVAssetExportSession 将视频导出到照片库
- python - 使用 PostBack 函数解析更改内容的 html 页面
- r - 使用 office R 添加图像时结合 paste0 和 src
- isabelle - Isabelle 在处理两个理论文件时“无法应用证明方法”
- scala - 当系统同时安装了 Scala2 和 Scala3 时如何执行 Scala3 脚本?
- angular - 在angular11中创建控件后如何动态设置验证器?