I have a problem with my recycler view scroll position. I used the solution from this link.
listViewModel.products.observe(viewLifecycleOwner, Observer {
val recyclerViewState = recyclerView.layoutManager?.onSaveInstanceState()
listAdapter.setProductList(it)
recyclerView.adapter = listAdapter
recyclerView.layoutManager?.onRestoreInstanceState(recyclerViewState)
It works but it seems like If the scroll is sometimes in the wrong position, the view "jumps" a bit during refresh live data. What might be a problem?
My recycler: recycler
EDIT
I read that the problem is probably checkbox. So here's my impl:
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)
}
}
And fragment
override fun onCheckBoxClick(position: Int, isChecked: Boolean) {
val clickedProduct: Produkt = listAdapter.getProductByPosition(position)
listaViewModel.updateProduct(clickedProduct, isChecked)
}