Skip to content

Commit

Permalink
fix: #409 修复单一展开模式下删除数据后展开导致崩溃
Browse files Browse the repository at this point in the history
  • Loading branch information
liangjingkanji committed Dec 3, 2023
1 parent ed3bd9e commit edcd124
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions brv/src/main/java/com/drake/brv/BindingAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ open class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolde
return when {
isHeader(position) -> headers[position] as M
isFooter(position) -> footers[position - headerCount - modelCount] as M
else -> models!!.let { it[position - headerCount] as M }
else -> models!![position - headerCount] as M
}
}

Expand Down Expand Up @@ -1014,11 +1014,7 @@ open class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolde
scrollTop: Boolean = false,
@IntRange(from = -1) depth: Int = 0,
): Int {
val holder = rv?.findViewHolderForLayoutPosition(position) as? BindingViewHolder ?: rv?.run {
val holder = createViewHolder(this, getItemViewType(position))
bindViewHolder(holder, position)
holder
} ?: return 0
val holder = getBindViewHolder(position) ?: return 0
return holder.expand(scrollTop, depth)
}

Expand All @@ -1029,11 +1025,7 @@ open class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolde
* @return 折叠后消失的条目数量
*/
fun collapse(@IntRange(from = 0) position: Int, @IntRange(from = -1) depth: Int = 0): Int {
val holder = rv?.findViewHolderForLayoutPosition(position) as? BindingViewHolder ?: rv?.run {
val holder = createViewHolder(this, getItemViewType(position))
bindViewHolder(holder, position)
holder
} ?: return 0
val holder = getBindViewHolder(position) ?: return 0
return holder.collapse(depth)
}

Expand All @@ -1048,14 +1040,26 @@ open class BindingAdapter : RecyclerView.Adapter<BindingAdapter.BindingViewHolde
scrollTop: Boolean = false,
@IntRange(from = -1) depth: Int = 0,
): Int {
val holder = rv?.findViewHolderForLayoutPosition(position) as? BindingViewHolder ?: rv?.run {
val holder = createViewHolder(this, getItemViewType(position))
bindViewHolder(holder, position)
holder
} ?: return 0
val holder = getBindViewHolder(position) ?: return 0
return holder.expandOrCollapse(scrollTop, depth)
}

/**
* 获取ViewHolder, 如果获取不到就创建一个
*/
private fun getBindViewHolder(position: Int): BindingViewHolder? {
val rv = rv ?: return null
return rv.findViewHolderForLayoutPosition(position) as? BindingViewHolder ?: run {
try {
val holder = createViewHolder(rv, getItemViewType(position))
bindViewHolder(holder, position)
holder
} catch (e: Exception) {
null
}
}
}

//</editor-fold>

inner class BindingViewHolder : RecyclerView.ViewHolder {
Expand Down

0 comments on commit edcd124

Please sign in to comment.