[英]Saving scroll position RecyclerView
我的回收站视图滚动条 position 有问题。我使用了此链接中的解决方案。
listViewModel.products.observe(viewLifecycleOwner, Observer {
val recyclerViewState = recyclerView.layoutManager?.onSaveInstanceState()
listAdapter.setProductList(it)
recyclerView.adapter = listAdapter
recyclerView.layoutManager?.onRestoreInstanceState(recyclerViewState)
它有效,但似乎如果滚动有时在错误的 position 中,则在刷新实时数据期间视图会“跳跃”一点。 可能是什么问题?
我的回收站: recycler
编辑
我读到问题可能是复选框。 所以这是我的意思:
inner class ListViewHolder(val binding: ItemListBinding ) : RecyclerView.ViewHolder(binding.root), View.OnClickListener{
fun bind(product: Product) {
binding.checkBoxBought.isChecked = produkt.isBought == true
}
init{
binding.checkBoxBought.setOnClickListener(this)
}
override fun onClick(p0: View?) {
val position: Int = bindingAdapterPosition
if (position != RecyclerView.NO_POSITION) {
listener.onCheckBoxClick(position, binding.checkBoxBought.isChecked)
}
}
和片段
override fun onCheckBoxClick(position: Int, isChecked: Boolean) {
val clickedProduct: Produkt = listAdapter.getProductByPosition(position)
listaViewModel.updateProduct(clickedProduct, isChecked)
}
将此代码放入您的适配器 class,从适配器中设置复选框 true/false
yourCheckBox.setOnCheckedChangeListener(null);
yourCheckBox.setChecked(/*Get True/Flase From Model*/);
yourCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
//your check/uncheck code goes here....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.