Home > front end >  ListAdapter change items position after submitlist
ListAdapter change items position after submitlist

Time:11-29

After listAdapter load to recyclerview, some data change on items. I use submitList method to reload list. It updates list well but when do submitList it change position of items. For example: The upper one goes down. Lower one goes up. I can't find notify list without this movement.

CartFragment.kt

viewModel.increaseCart.observe(viewLifecycleOwner){
    it?.onLoading {  }
    it?.onSuccess {
        it?.data?.let { cart->
            initTotalFee(cart)
            adapter?.submitList(cart.cartItems)
        }
    }
    it?.onError { e->
        e
    }
}

How to change list without submitList method?

Just tried these.

adapter?.submitList(cart.cartItems)
adapter?.notifyDataSetChanged()

CodePudding user response:

I do not know what cartItems are but i assume that they are objects. If that object has a field of price or something comperable, than you can sort it and send to adapter. With this approach it will not randomized in adapter:

viewModel.increaseCart.observe(viewLifecycleOwner){
    it?.onLoading {  }
    it?.onSuccess {
        it?.data?.let { cart->
            initTotalFee(cart)
            val sortedList = cart.cartItems.sortedWith(compareBy({ it.price }))
            adapter?.submitList(sortedList)
        }
    }
    it?.onError { e->
        e
    }
}
  • Related