Always re-bind CandidateViewHolder in PagingCandidateViewAdapter

the adapter would skip onBindViewHolder when diffCallback determines areItemsTheSame
but we should always update the candidate idx
This commit is contained in:
Rocka 2026-07-04 23:08:53 +08:00
parent 1d13d2e992
commit 35229d1258
No known key found for this signature in database
GPG Key ID: 28031158FFDD6853
2 changed files with 13 additions and 14 deletions

View File

@ -1,6 +1,6 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* SPDX-FileCopyrightText: Copyright 2024 Fcitx5 for Android Contributors
* SPDX-FileCopyrightText: Copyright 2024-2026 Fcitx5 for Android Contributors
*/
package org.fcitx.fcitx5.android.input.candidates
@ -17,13 +17,13 @@ class CandidateViewHolder(val ui: CandidateItemUi) : RecyclerView.ViewHolder(ui.
fun update(newIndex: Int, newCandidate: CandidateWord) {
idx = newIndex
candidate = newCandidate
ui.updateCandidate(newCandidate)
if (candidate != newCandidate) {
candidate = newCandidate
ui.updateCandidate(newCandidate)
}
}
fun clear() {
idx = -1
candidate = CandidateWord.Empty
ui.updateCandidate(CandidateWord.Empty)
update(-1, CandidateWord.Empty)
}
}

View File

@ -1,6 +1,6 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* SPDX-FileCopyrightText: Copyright 2021-2024 Fcitx5 for Android Contributors
* SPDX-FileCopyrightText: Copyright 2021-2026 Fcitx5 for Android Contributors
*/
package org.fcitx.fcitx5.android.input.candidates.expanded
@ -17,14 +17,13 @@ open class PagingCandidateViewAdapter(val theme: Theme) :
PagingDataAdapter<CandidateWord, CandidateViewHolder>(diffCallback) {
companion object {
/**
* Always re-bind all [CandidateViewHolder]s every time to make sure `idx` is up-to-date.
* [CandidateViewHolder.update] would skip unnecessary UI updates.
*/
private val diffCallback = object : DiffUtil.ItemCallback<CandidateWord>() {
override fun areItemsTheSame(oldItem: CandidateWord, newItem: CandidateWord): Boolean {
return oldItem.text == newItem.text && oldItem.comment == newItem.comment
}
override fun areContentsTheSame(oldItem: CandidateWord, newItem: CandidateWord): Boolean {
return oldItem == newItem
}
override fun areItemsTheSame(oldItem: CandidateWord, newItem: CandidateWord) = false
override fun areContentsTheSame(oldItem: CandidateWord, newItem: CandidateWord) = false
}
}