我有垂直 recyclerView,几乎没有嵌套的水平 recyclerView。每个嵌套列表都通过 DiffUtils 机制动态更新。
当我更新嵌套列表时,我等待滚动保持在相同的位置并且新项目出现在它的右侧,但滚动在页面之前。到底是怎么回事?
class Adapter : RecyclerView.Adapter {
private var nestedItems: List<Item> = emptyList()
fun updateNestedList(items: List<Item>) {
val diff = DiffUtil.calculateDiff(...)
nestedItems = items
diff.dispatchUpdatesTo(this)
}
override fun onCreateViewHolder() {
when (itemType) {
...
ITEM_TYPE_HORIZONTAL -> NestedListViewHolder()
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
...
is NestedListViewHolder -> (holder as NestedListViewHolder).bind(items)
}
}
}
class NestedListViewHolder : RecyclerView.ViewHolder {
private val adapter by lazy {
NestedAdapter().apply {
val layoutManager = LinearLayoutManager(containerView.context, LinearLayoutManager.HORIZONTAL, reverseLayout = false)
contacts_list.layoutManager = layoutManager
contacts_list.isNestedScrollingEnabled = false
contacts_list.adapter = this
}
}
fun bind(items: List<Item>) {
adapter.updateContacts(items)
}
}
class NestedAdapter : RecyclerView.Adapter {
private var items: List<Item> = emptyList()
fun updateItems(_items: List<Item>) {
val diff = DiffUtil.calculateDiff(...)
items = _items
diff.dispatchUpdatesTo(this)
}
...
}