Skip to content

Commit 107bce3

Browse files
committed
fix: 数据边界问题
1 parent c514f7a commit 107bce3

2 files changed

Lines changed: 17 additions & 23 deletions

File tree

library/src/main/java/com/chad/library/adapter4/BaseQuickAdapter.kt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ abstract class BaseQuickAdapter<T : Any, VH : RecyclerView.ViewHolder>(
208208

209209
if (oldDisplayEmptyLayout && !newDisplayEmptyLayout) {
210210
notifyItemRemoved(0)
211-
recyclerView.scrollToPosition(0)
211+
_recyclerView?.scrollToPosition(0)
212212
} else if (newDisplayEmptyLayout && !oldDisplayEmptyLayout) {
213213
notifyItemInserted(0)
214214
} else if (oldDisplayEmptyLayout && newDisplayEmptyLayout) {
@@ -351,7 +351,8 @@ abstract class BaseQuickAdapter<T : Any, VH : RecyclerView.ViewHolder>(
351351
override fun onViewAttachedToWindow(holder: RecyclerView.ViewHolder) {
352352
super.onViewAttachedToWindow(holder)
353353

354-
if (holder is StateLayoutVH || isFullSpanItem(getItemViewType(holder.bindingAdapterPosition))) {
354+
val position = holder.bindingAdapterPosition
355+
if (holder is StateLayoutVH || (position != RecyclerView.NO_POSITION && isFullSpanItem(getItemViewType(position)))) {
355356
holder.asStaggeredGridFullSpan()
356357
} else {
357358
runAnimator(holder)
@@ -661,7 +662,7 @@ abstract class BaseQuickAdapter<T : Any, VH : RecyclerView.ViewHolder>(
661662
payload: Any? = null,
662663
commitCallback: Runnable? = null,
663664
) {
664-
if (position >= items.size) {
665+
if (position !in items.indices) {
665666
throw IndexOutOfBoundsException("position: ${position}. size:${items.size}")
666667
}
667668

@@ -810,7 +811,7 @@ abstract class BaseQuickAdapter<T : Any, VH : RecyclerView.ViewHolder>(
810811
*/
811812
@JvmOverloads
812813
open fun removeAt(@IntRange(from = 0) position: Int, commitCallback: Runnable? = null) {
813-
if (position >= items.size) {
814+
if (position !in items.indices) {
814815
throw IndexOutOfBoundsException("position: ${position}. size:${items.size}")
815816
}
816817

@@ -863,7 +864,7 @@ abstract class BaseQuickAdapter<T : Any, VH : RecyclerView.ViewHolder>(
863864
if (range.isEmpty()) {
864865
return
865866
}
866-
if (range.first >= items.size) {
867+
if (range.first !in items.indices) {
867868
throw IndexOutOfBoundsException("Range first position: ${range.first} - last position: ${range.last}. size:${items.size}")
868869
}
869870

@@ -902,7 +903,7 @@ abstract class BaseQuickAdapter<T : Any, VH : RecyclerView.ViewHolder>(
902903
open fun swap(fromPosition: Int, toPosition: Int, commitCallback: Runnable? = null) {
903904
if (mDiffer == null) {
904905
if (fromPosition in _items.indices && toPosition in _items.indices) {
905-
Collections.swap(_items, fromPosition, toPosition)
906+
Collections.swap(mutableItems, fromPosition, toPosition)
906907
notifyItemChanged(fromPosition)
907908
notifyItemChanged(toPosition)
908909

@@ -938,7 +939,7 @@ abstract class BaseQuickAdapter<T : Any, VH : RecyclerView.ViewHolder>(
938939
}
939940
} else {
940941
val list = mDiffer.currentList
941-
if (fromPosition in list.indices || toPosition in list.indices) {
942+
if (fromPosition in list.indices && toPosition in list.indices) {
942943
list.toMutableList().also {
943944
val e = it.removeAt(fromPosition)
944945
it.add(toPosition, e)
@@ -953,16 +954,10 @@ abstract class BaseQuickAdapter<T : Any, VH : RecyclerView.ViewHolder>(
953954
*/
954955
private val mutableItems: MutableList<T>
955956
get() {
956-
return when (_items) {
957-
is java.util.AbstractList -> {
958-
_items as java.util.AbstractList
959-
}
960-
is MutableList -> {
961-
_items as MutableList
962-
}
963-
else -> {
964-
_items.toMutableList().apply { _items = this }
965-
}
957+
return if (_items is ArrayList<T>) {
958+
_items as ArrayList<T>
959+
} else {
960+
ArrayList(_items).apply { _items = this }
966961
}
967962
}
968963

library/src/main/java/com/chad/library/adapter4/QuickAdapterHelper.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ class QuickAdapterHelper private constructor(
183183
index + 1
184184
}
185185

186-
if (mAdapter.addAdapter(realIndex, adapter)) {
187-
mBeforeList += adapter
188-
}
186+
if (mAdapter.addAdapter(realIndex, adapter)) {
187+
mBeforeList.add(index, adapter)
188+
}
189189
}
190190

191191
/**
@@ -251,8 +251,8 @@ class QuickAdapterHelper private constructor(
251251
mAdapter.adapters.size - 1 - mAfterList.size + index
252252
}
253253

254-
if(mAdapter.addAdapter(realIndex, adapter)) {
255-
mAfterList += adapter
254+
if (mAdapter.addAdapter(realIndex, adapter)) {
255+
mAfterList.add(index, adapter)
256256
}
257257
}
258258

@@ -442,4 +442,3 @@ class QuickAdapterHelper private constructor(
442442
}
443443
}
444444

445-

0 commit comments

Comments
 (0)