I have a recyclerView of elements that contain checkboxes. Currently when a checkbox is checked it goes to the last position and then if it is unchecked it goes to the top position (index 0). Therefore all unchecked are in a top section and those checked in a bottom section. I would like for it however to not go directly to the top or bottom when check/uncheck but to go to it's sorted position based on two criteria from the data class (checked(boolean) and order(int)).
For a clarification example I have 10 elements. There are currently 3 checked elements in the bottom section (who have order@ 1,3,5 respectively) and I just checked a new element (whose order# is 2) I want that element to go to the "checked" section and go directly to the correct slot (1,2,3,5) where 5 is in the last position. The same should go for unchecking an element. If I uncheck the element with order# 5, it should go to the top section of unchecked elements and go directly to it's correct order position of unchecked elements like (4,5,6,7,8,9,10).
In Adapter.kt
These are the methods I currently have:
fun unchecked() {
val toPosition = list.size - 1
val item = list[holder.position]
list.removeAt(holder.position)
list.add(toPosition, item)
notifyItemMoved(holder.position, toPosition)
}
fun checked() {
val toPosition = 0
val item = list[holder.position]
list.removeAt(holder.position)
list.add(toPosition, item)
notifyItemMoved(holder.position, toPosition)
}
I have tried to implement the following in within the above methods or as a standalone method called after and other ways, but getting issues where the elements duplicate or don't reflect their proper position and other weird stuff.
fun sort() {
list.sortWith(compareBy<Model> { it.checked }.thenBy { it.order})
notifyDataSetChanged()
}
Thanks for any suggestions.