Skip to content

Commit

Permalink
fix: random crash on select LiquidKeyboard tab
Browse files Browse the repository at this point in the history
RecycleView's notifyItemRangeChanged may has bugs, use notifyItemRangeRemoved and notifyItemRangeInserted.
  • Loading branch information
WhiredPlanck committed Feb 14, 2024
1 parent c1e3dfc commit 696a259
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class CandidateAdapter(private val theme: Theme) : RecyclerView.Adapter<Candidat
}

fun updateCandidates(candidates: List<CandidateListItem>) {
val prevSize = mCandidates.size
mCandidates.clear()
notifyItemRangeRemoved(0, prevSize)
mCandidates.addAll(candidates)
notifyItemRangeChanged(0, candidates.size)
notifyItemRangeInserted(0, candidates.size)
}

override fun getItemCount(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ class FlexibleAdapter(
else -> b2.id.compareTo(b1.id)
}
}
val prevSize = mBeans.size
mBeans.clear()
notifyItemRangeRemoved(0, prevSize)
mBeans.addAll(sorted)
notifyItemRangeChanged(0, sorted.size)
mBeansId.clear()
mBeans.forEachIndexed { index: Int, (id): DatabaseBean ->
mBeansId[id] = index
}
// 更新视图
notifyItemRangeChanged(0, mBeans.size)
}

override fun getItemCount(): Int = mBeans.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ class SimpleAdapter(private val theme: Theme, private val columnSize: Int) : Rec
get() = mBeans

fun updateBeans(beans: List<SimpleKeyBean>) {
val prevSize = mBeans.size
mBeans.clear()
notifyItemRangeRemoved(0, prevSize)
mBeans.addAll(beans)
notifyItemRangeInserted(0, beans.size)
mBeansByRows.clear()
mBeansByRows.addAll(beans.chunked(columnSize))
notifyItemRangeChanged(0, mBeans.size)
}

override fun getItemCount(): Int {
Expand Down

0 comments on commit 696a259

Please sign in to comment.