0

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)

}
Simon
  • 55
  • 1
  • 6

1 Answers1

0

Put this code in your adapter class, set check box true/false from adapter

yourCheckBox.setOnCheckedChangeListener(null);
yourCheckBox.setChecked(/*Get True/Flase From Model*/);
yourCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
    //your check/uncheck code goes here....
}
                                
Kishan Mevada
  • 662
  • 1
  • 6
  • 17
  • Im not using onCheckedChangeListener. State of the checkbox depends on boolean value from database. I have implemented the on click function that updates that status. Everything happens in view holder class. – Simon May 30 '22 at 18:18