Skip to content

Commit

Permalink
修复侧滑删除遇到有 header 的时候移除 models 中 item 下标越界的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
mcxinyu authored and liangjingkanji committed Sep 5, 2022
1 parent c0dbf9a commit 831cc39
Showing 1 changed file with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.view.View
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
import com.drake.brv.BindingAdapter
import com.drake.brv.BindingAdapter.BindingViewHolder
import com.drake.brv.item.ItemDrag
import com.drake.brv.item.ItemSwipe
import com.drake.brv.utils.bindingAdapter
Expand All @@ -32,22 +33,27 @@ open class DefaultItemTouchCallback : ItemTouchHelper.Callback() {

/** 侧滑到底item消失时 */
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val adapter = viewHolder.bindingAdapter as? BindingAdapter
val adapter = viewHolder.bindingAdapter as? BindingAdapter ?: return
val layoutPosition = viewHolder.layoutPosition
adapter?.notifyItemRemoved(layoutPosition)
(adapter?.models as ArrayList).removeAt(layoutPosition)
val headerCount = adapter.headerCount
if (layoutPosition < headerCount) {
adapter.removeHeader(layoutPosition, true)
} else {
adapter.mutable.removeAt(layoutPosition - headerCount)
adapter.notifyItemRemoved(layoutPosition)
}
}

/**
* 返回值表示拖拽/侧滑的方向
* @param viewHolder 拖拽触发的Item
*/
override fun getMovementFlags(
recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder
recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder,
): Int {
var drag = 0
var swipe = 0
if (viewHolder is BindingAdapter.BindingViewHolder) {
if (viewHolder is BindingViewHolder) {
val model = viewHolder.getModel<Any>()
if (model is ItemDrag) drag = model.itemOrientationDrag
if (model is ItemSwipe) swipe = model.itemOrientationSwipe
Expand All @@ -63,7 +69,7 @@ open class DefaultItemTouchCallback : ItemTouchHelper.Callback() {
dX: Float,
dY: Float,
actionState: Int,
isCurrentlyActive: Boolean
isCurrentlyActive: Boolean,
) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
val swipeView = viewHolder.itemView.findViewWithTag<View>("swipe")
Expand All @@ -90,15 +96,13 @@ open class DefaultItemTouchCallback : ItemTouchHelper.Callback() {
* @param source 触发拖拽的Item
* @param target 拖拽目标的Item
*/
open fun onDrag(
source: BindingAdapter.BindingViewHolder, target: BindingAdapter.BindingViewHolder
) {
open fun onDrag(source: BindingViewHolder, target: BindingViewHolder) {

}

private var lastActionState: Int = 0
private var sourceViewHolder: BindingAdapter.BindingViewHolder? = null
private var targetViewHolder: BindingAdapter.BindingViewHolder? = null
private var sourceViewHolder: BindingViewHolder? = null
private var targetViewHolder: BindingViewHolder? = null

/**
* 状态变化
Expand All @@ -111,8 +115,9 @@ open class DefaultItemTouchCallback : ItemTouchHelper.Callback() {
when (actionState) {
ItemTouchHelper.ACTION_STATE_IDLE -> {
if (lastActionState == ItemTouchHelper.ACTION_STATE_DRAG &&
sourceViewHolder is BindingAdapter.BindingViewHolder &&
targetViewHolder is BindingAdapter.BindingViewHolder) {
sourceViewHolder is BindingViewHolder &&
targetViewHolder is BindingViewHolder
) {
onDrag(sourceViewHolder!!, targetViewHolder!!)
}
}
Expand All @@ -124,13 +129,15 @@ open class DefaultItemTouchCallback : ItemTouchHelper.Callback() {

/** 拖拽移动超过其他item时, 其返回值表示是否已经拖拽替换(会触发函数onMoved) */
override fun onMove(
recyclerView: RecyclerView, source: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder
recyclerView: RecyclerView,
source: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder,
): Boolean {
val adapter = recyclerView.bindingAdapter as? BindingAdapter ?: return false
val currentPosition = recyclerView.getChildLayoutPosition(source.itemView)
val targetPosition = recyclerView.getChildLayoutPosition(target.itemView)

if (source is BindingAdapter.BindingViewHolder && target is BindingAdapter.BindingViewHolder) {
if (source is BindingViewHolder && target is BindingViewHolder) {
val model = target.getModel<Any>()
if (model is ItemDrag && model.itemOrientationDrag != 0) {
val fromPosition = currentPosition - adapter.headerCount
Expand Down

0 comments on commit 831cc39

Please sign in to comment.