I am using List Adapter to display a list of strings.
Whenever I update data on the recycler view it scrolls the data to the end of the list
Any idea what is causing this?
for reference below is the code for the adapter
class TestAdapter(private val taskListener: DetailTaskAdapterListener) : ListAdapter<Task, TestAdapter.TestViewHolder>(DiffUtilCallback) {
inner class TestViewHolder(private val binding: DetailSubTaskItemBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(task: Task) {
binding.checkbox.isChecked = task.isComplete
binding.task.setText(task.task.trim())
}
fun setupListener() {
binding.checkbox.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
binding.task.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
} else {
binding.task.paintFlags = 0
}
getItem(absoluteAdapterPosition).copy(isComplete = isChecked)
}
binding.task.setOnFocusChangeListener { view, hasFocus ->
if(!hasFocus){
if(binding.task.text.toString().trim().isEmpty()){
taskListener.deleteTask(getItem(absoluteAdapterPosition).taskId)
}else{
taskListener.updateTask(getItem(absoluteAdapterPosition).taskId, binding.task.text.toString().trim())
}
}
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TestViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
return TestViewHolder(
DetailSubTaskItemBinding.inflate(
layoutInflater, parent, false
)
)
}
override fun onBindViewHolder(holder: TestViewHolder, position: Int) {
val task = getItem(position)
holder.setupListener()
holder.bind(task)
}
companion object DiffUtilCallback : DiffUtil.ItemCallback<Task>() {
override fun areItemsTheSame(oldItem: Task, newItem: Task): Boolean {
return oldItem.task == newItem.task
}
override fun areContentsTheSame(oldItem: Task, newItem: Task): Boolean {
return oldItem == newItem
}
}
}
code below is the DAO, (note: both insert and update function causes recyclerview to push updated data to the last row)
@Transaction
@Query("SELECT * FROM todo WHERE todoId = :todoId")
fun getTodoDetails(todoId: String): Flow<TodoDetailsEntity>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertTask(task: TaskEntity): Long
@Query("UPDATE task SET task = :title WHERE taskId = :taskId")
suspend fun updateTaskTitle(taskId: String, title: String): Int
Below is the entity model
data class TodoDetailsEntity(
@Embedded val todo: TodoEntity,
@Relation(
parentColumn = "todoId",
entityColumn = "todoRefId"
)
val tasks: List<TaskEntity>,
@Relation(
parentColumn = "todoId",
entityColumn = "noteId"
)
val note: NoteEntity?,
@Relation(
parentColumn = "todoId",
entityColumn = "todoRefId"
)
val attachments: List<AttachmentEntity>
)
submitList is called in Activity onCreate
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.saveTodoId(intent.extras?.getString("todoId")!!)
todoId = intent.extras?.getString("todoId")
binding = ActivityDetailsBinding.inflate(layoutInflater)
setContentView(binding.root)
setupToolbar()
setupTestAdapter()
collectLatestLifecycleFlow(viewModel.uiState) { uiState ->
testAdapter.submitList(uiState.todoDetails?.tasks)
}
}
For reference, whenever I update the data to database, ROOM will automatically move the data to the last index
Before updating