mirror of
https://github.com/fcitx5-android/fcitx5-android.git
synced 2026-08-02 20:16:06 +08:00
Implement "stable ID" for candidate adapters
prevent crash in FlexboxLayoutManager and hopefully improves performance ref: https://github.com/google/flexbox-layout/issues/363#issuecomment-382949953
This commit is contained in:
parent
cf4fb49edc
commit
bc2d6735f5
@ -43,6 +43,13 @@ class PagedCandidatesUi(
|
||||
}
|
||||
|
||||
private val candidatesAdapter = object : RecyclerView.Adapter<UiHolder>() {
|
||||
init {
|
||||
setHasStableIds(true)
|
||||
}
|
||||
|
||||
override fun getItemId(position: Int): Long =
|
||||
data.candidates[position].hashCode().toLong()
|
||||
|
||||
override fun getItemCount() =
|
||||
data.candidates.size + (if (data.hasPrev || data.hasNext) 1 else 0)
|
||||
|
||||
@ -98,6 +105,7 @@ class PagedCandidatesUi(
|
||||
adapter = candidatesAdapter
|
||||
layoutManager = candidatesLayoutManager
|
||||
overScrollMode = View.OVER_SCROLL_NEVER
|
||||
itemAnimator = null
|
||||
}
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
|
||||
@ -163,6 +163,7 @@ class HorizontalCandidateComponent :
|
||||
}
|
||||
}.apply {
|
||||
id = R.id.candidate_view
|
||||
itemAnimator = null
|
||||
adapter = this@HorizontalCandidateComponent.adapter
|
||||
layoutManager = this@HorizontalCandidateComponent.layoutManager
|
||||
addItemDecoration(FlexboxVerticalDecoration(dividerDrawable))
|
||||
|
||||
@ -21,6 +21,10 @@ import splitties.views.setPaddingDp
|
||||
open class HorizontalCandidateViewAdapter(val theme: Theme) :
|
||||
RecyclerView.Adapter<CandidateViewHolder>() {
|
||||
|
||||
init {
|
||||
setHasStableIds(true)
|
||||
}
|
||||
|
||||
var candidates: Array<String> = arrayOf()
|
||||
private set
|
||||
|
||||
@ -36,6 +40,10 @@ open class HorizontalCandidateViewAdapter(val theme: Theme) :
|
||||
|
||||
override fun getItemCount() = candidates.size
|
||||
|
||||
override fun getItemId(position: Int): Long {
|
||||
return candidates[position].hashCode().toLong()
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CandidateViewHolder {
|
||||
val ui = CandidateItemUi(parent.context, theme)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user