Skip to content

Commit

Permalink
fix: 交换元素产生的数组顺序错误问题
Browse files Browse the repository at this point in the history
  • Loading branch information
limuyang2 committed Aug 2, 2023
1 parent ae1e4b0 commit b5bc77c
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.baserecyclerviewadapterhelper.activity.dragswipe.adapter.DragAndSwipeAdapter
import com.chad.baserecyclerviewadapterhelper.base.BaseViewBindingActivity
import com.chad.baserecyclerviewadapterhelper.databinding.ActivityUniversalRecyclerBinding
import com.chad.baserecyclerviewadapterhelper.utils.Tips
import com.chad.baserecyclerviewadapterhelper.utils.VibratorUtils.vibrate
import com.chad.baserecyclerviewadapterhelper.utils.vibrate
import com.chad.library.adapter.base.dragswipe.QuickDragAndSwipe
import com.chad.library.adapter.base.dragswipe.listener.OnItemDragListener
import com.chad.library.adapter.base.dragswipe.listener.OnItemSwipeListener
Expand All @@ -28,7 +28,8 @@ class DefaultDragAndSwipeActivity : BaseViewBindingActivity<ActivityUniversalRec
private val mAdapter: DragAndSwipeAdapter = DragAndSwipeAdapter()

private val quickDragAndSwipe = QuickDragAndSwipe()
.setDragMoveFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN)
.setDragMoveFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN or
ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
.setSwipeMoveFlags(ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)

override fun initBinding(): ActivityUniversalRecyclerBinding =
Expand All @@ -39,7 +40,7 @@ class DefaultDragAndSwipeActivity : BaseViewBindingActivity<ActivityUniversalRec
viewBinding.titleBar.title = "Default Drag And Swipe"
viewBinding.titleBar.setOnBackListener { finish() }

viewBinding.rv.layoutManager = LinearLayoutManager(this)
viewBinding.rv.layoutManager = GridLayoutManager(this,3)
viewBinding.rv.adapter = mAdapter

val mData = generateData(50)
Expand All @@ -48,7 +49,7 @@ class DefaultDragAndSwipeActivity : BaseViewBindingActivity<ActivityUniversalRec
// 拖拽监听
val listener: OnItemDragListener = object : OnItemDragListener {
override fun onItemDragStart(viewHolder: RecyclerView.ViewHolder?, pos: Int) {
vibrate(applicationContext)
vibrate()
Log.d(TAG, "drag start")
val holder = viewHolder as QuickViewHolder? ?: return
// 开始时,item背景色变化,demo这里使用了一个动画渐变,使得自然
Expand Down Expand Up @@ -94,6 +95,14 @@ class DefaultDragAndSwipeActivity : BaseViewBindingActivity<ActivityUniversalRec
v.duration = 300
v.start()
}

mAdapter.items.forEach {
Log.d(
TAG,
"-------->> w 顺序 ${it} "
)
}

}
}
val swipeListener: OnItemSwipeListener = object : OnItemSwipeListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.chad.baserecyclerviewadapterhelper.activity.dragswipe.adapter.DiffDra
import com.chad.baserecyclerviewadapterhelper.base.BaseViewBindingActivity
import com.chad.baserecyclerviewadapterhelper.data.DataServer
import com.chad.baserecyclerviewadapterhelper.databinding.ActivityUniversalRecyclerBinding
import com.chad.baserecyclerviewadapterhelper.utils.VibratorUtils.vibrate
import com.chad.baserecyclerviewadapterhelper.utils.vibrate
import com.chad.library.adapter.base.dragswipe.QuickDragAndSwipe
import com.chad.library.adapter.base.dragswipe.listener.DragAndSwipeDataCallback
import com.chad.library.adapter.base.dragswipe.listener.OnItemDragListener
Expand All @@ -30,7 +30,7 @@ class DragAndSwipeDifferActivity : BaseViewBindingActivity<ActivityUniversalRecy

private var mAdapter: DiffDragAndSwipeAdapter = DiffDragAndSwipeAdapter()

var quickDragAndSwipe = QuickDragAndSwipe()
private var quickDragAndSwipe = QuickDragAndSwipe()
.setDragMoveFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN)
.setSwipeMoveFlags(ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)

Expand Down Expand Up @@ -61,7 +61,7 @@ class DragAndSwipeDifferActivity : BaseViewBindingActivity<ActivityUniversalRecy
val listener: OnItemDragListener = object :
OnItemDragListener {
override fun onItemDragStart(viewHolder: RecyclerView.ViewHolder?, pos: Int) {
vibrate(applicationContext)
vibrate()
Log.d(TAG, "drag start")
val holder = viewHolder as QuickViewHolder
// 开始时,item背景色变化,demo这里使用了一个动画渐变,使得自然
Expand Down Expand Up @@ -138,7 +138,7 @@ class DragAndSwipeDifferActivity : BaseViewBindingActivity<ActivityUniversalRecy

quickDragAndSwipe.attachToRecyclerView(viewBinding.rv)
.setDataCallback(object : DragAndSwipeDataCallback {
override fun dataSwap(fromPosition: Int, toPosition: Int) {
override fun dataMove(fromPosition: Int, toPosition: Int) {
mAdapter.swap(fromPosition, toPosition)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ import com.chad.baserecyclerviewadapterhelper.activity.dragswipe.adapter.HeaderD
import com.chad.baserecyclerviewadapterhelper.activity.home.adapter.HomeTopHeaderAdapter
import com.chad.baserecyclerviewadapterhelper.base.BaseViewBindingActivity
import com.chad.baserecyclerviewadapterhelper.databinding.ActivityUniversalRecyclerBinding
import com.chad.baserecyclerviewadapterhelper.utils.VibratorUtils.vibrate
import com.chad.baserecyclerviewadapterhelper.utils.vibrate
import com.chad.library.adapter.base.QuickAdapterHelper
import com.chad.library.adapter.base.dragswipe.setItemDragListener
import com.chad.library.adapter.base.dragswipe.setItemSwipeListener
import com.chad.library.adapter.base.loadState.LoadState.NotLoading
import com.chad.library.adapter.base.loadState.trailing.TrailingLoadStateAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
* 带头部局以及带加载下一页的,拖拽demo
Expand Down Expand Up @@ -83,7 +87,7 @@ class HeaderDragAndSwipeActivity : BaseViewBindingActivity<ActivityUniversalRecy
.setItemDragListener(
onItemDragStart = { viewHolder, pos ->
Log.d(TAG, "drag start")
vibrate(applicationContext)
vibrate()
val holder = viewHolder as QuickViewHolder
// 开始时,item背景色变化,demo这里使用了一个动画渐变,使得自然
val startColor = Color.WHITE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.chad.baserecyclerviewadapterhelper.base.BaseViewBindingActivity;
import com.chad.baserecyclerviewadapterhelper.databinding.ActivityUniversalRecyclerBinding;
import com.chad.baserecyclerviewadapterhelper.utils.Tips;
import com.chad.baserecyclerviewadapterhelper.utils.VibratorUtils;
import com.chad.baserecyclerviewadapterhelper.utils.VibratorUtilsKt;
import com.chad.library.adapter.base.QuickAdapterHelper;
import com.chad.library.adapter.base.dragswipe.QuickDragAndSwipe;
import com.chad.library.adapter.base.dragswipe.listener.OnItemDragListener;
Expand Down Expand Up @@ -63,7 +63,7 @@ protected void onCreate(Bundle savedInstanceState) {
OnItemDragListener listener = new OnItemDragListener() {
@Override
public void onItemDragStart(@Nullable RecyclerView.ViewHolder viewHolder, int pos) {
VibratorUtils.INSTANCE.vibrate(getApplicationContext());
VibratorUtilsKt.vibrate(getApplicationContext());
Log.d(TAG, "drag start");
final QuickViewHolder holder = ((QuickViewHolder) viewHolder);
if (holder == null) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,18 @@ protected QuickViewHolder onCreateViewHolder(@NonNull Context context, @NonNull
@Override
protected void onBindViewHolder(@NonNull QuickViewHolder holder, int position, String item) {
switch (holder.getLayoutPosition() % 3) {
case 0:
holder.setImageResource(R.id.iv_head, R.mipmap.head_img0);
break;
case 1:
holder.setImageResource(R.id.iv_head, R.mipmap.head_img1);
break;
case 2:
holder.setImageResource(R.id.iv_head, R.mipmap.head_img2);
break;
default:
break;
case 0 -> holder.setImageResource(R.id.iv_head, R.mipmap.head_img0);
case 1 -> holder.setImageResource(R.id.iv_head, R.mipmap.head_img1);
case 2 -> holder.setImageResource(R.id.iv_head, R.mipmap.head_img2);
default -> {
}
}
holder.setText(R.id.tv, item);
}

@Override
public void dataSwap(int fromPosition, int toPosition) {
swap(fromPosition, toPosition);
public void dataMove(int fromPosition, int toPosition) {
move(fromPosition, toPosition);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ open class HeaderDragAndSwipeAdapter : BaseQuickAdapter<String, QuickViewHolder>
holder.setText(R.id.tv, item)
}

override fun dataSwap(fromPosition: Int, toPosition: Int) {
swap(fromPosition, toPosition)
override fun dataMove(fromPosition: Int, toPosition: Int) {
move(fromPosition, toPosition)
}

override fun dataRemoveAt(position: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,21 @@ import android.os.Vibrator
import android.os.VibratorManager

/**
* 震动类
* 震动
*/
object VibratorUtils {
/**
* * 轻微的震动提醒
*/
fun vibrate(context: Context) {
if (Build.VERSION.SDK_INT >= 31) {
// android 12 及以上使用新的 VibratorManager,创建 EFFECT_TICK 轻微震动(需要线性震动马达硬件支持)
val manager: VibratorManager =
context.getSystemService<VibratorManager>(VibratorManager::class.java)
manager.defaultVibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK))
} else if (Build.VERSION.SDK_INT >= 29) {
// android 10 及以上使用原 Vibrator,创建 EFFECT_TICK 轻微震动(需要线性震动马达硬件支持)
val vib = context.getSystemService<Vibrator>(Vibrator::class.java) as Vibrator
vib.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK))
} else {
// 10 以下的系统,没有系统 API 驱动线性震动马达,只能创建普通震动
val vib = context.getSystemService(Service.VIBRATOR_SERVICE) as Vibrator //震动70毫秒
vib.vibrate(70)
}
fun Context.vibrate() {
if (Build.VERSION.SDK_INT >= 31) {
// android 12 及以上使用新的 VibratorManager,创建 EFFECT_TICK 轻微震动(需要线性震动马达硬件支持)
val manager: VibratorManager =
getSystemService(VibratorManager::class.java)
manager.defaultVibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK))
} else if (Build.VERSION.SDK_INT >= 29) {
// android 10 及以上使用原 Vibrator,创建 EFFECT_TICK 轻微震动(需要线性震动马达硬件支持)
val vib = getSystemService(Vibrator::class.java) as Vibrator
vib.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK))
} else {
// 10 以下的系统,没有系统 API 驱动线性震动马达,只能创建普通震动
val vib = getSystemService(Service.VIBRATOR_SERVICE) as Vibrator //震动70毫秒
vib.vibrate(70)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,21 @@ abstract class BaseDifferAdapter<T, VH : RecyclerView.ViewHolder>(
}

override fun swap(fromPosition: Int, toPosition: Int) {
val size = items.size
if (fromPosition in 0 until size || toPosition in 0 until size) {
if (fromPosition in items.indices || toPosition in items.indices) {
items.toMutableList().also {
Collections.swap(it, fromPosition, toPosition)
submitList(it)
}
}
}

override fun move(fromPosition: Int, toPosition: Int) {
if (fromPosition in items.indices || toPosition in items.indices) {
items.toMutableList().also {
val e = it.removeAt(fromPosition)
it.add(toPosition, e)
submitList(it)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -623,15 +623,30 @@ abstract class BaseQuickAdapter<T, VH : RecyclerView.ViewHolder>(

/**
* Item swap
* 数据位置交换
* 数据位置交换。这里单纯的只是两个数据交换位置。(注意⚠️,这里移动后的数据顺序与 [move] 不同)
*
* @param fromPosition
* @param toPosition
*/
open fun swap(fromPosition: Int, toPosition: Int) {
val size = items.size
if (fromPosition in 0 until size && toPosition in 0 until size) {
if (fromPosition in items.indices && toPosition in items.indices) {
Collections.swap(items, fromPosition, toPosition)
notifyItemChanged(fromPosition)
notifyItemChanged(toPosition)
}
}

/**
* Move Item
* item 位置的移动。(注意⚠️,这里移动后的数据顺序与 [swap] 不同)
*
* @param fromPosition
* @param toPosition
*/
open fun move(fromPosition: Int, toPosition: Int) {
if (fromPosition in items.indices && toPosition in items.indices) {
val e = mutableItems.removeAt(fromPosition)
mutableItems.add(toPosition, e)
notifyItemMoved(fromPosition, toPosition)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ open class QuickDragAndSwipe : ItemTouchHelper.Callback() {
if (fromPosition == RecyclerView.NO_POSITION || toPosition == RecyclerView.NO_POSITION) return

// 进行位置的切换
_dataCallback?.dataSwap(fromPosition, toPosition)
_dataCallback?.dataMove(fromPosition, toPosition)
mOnItemDragListener?.onItemDragMoving(viewHolder, fromPosition, target, toPosition)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.chad.library.adapter.base.dragswipe.listener

/**
* 由外部实现的数据操作
*/
interface DragAndSwipeDataCallback {

/**
* item 数据交换
*/
fun dataSwap(fromPosition: Int, toPosition: Int)
fun dataMove(fromPosition: Int, toPosition: Int)

/**
* 删除 Item 数据
Expand Down

0 comments on commit b5bc77c

Please sign in to comment.