Skip to content

Commit b5bc77c

Browse files
committed
fix: 交换元素产生的数组顺序错误问题
1 parent ae1e4b0 commit b5bc77c

File tree

11 files changed

+85
-56
lines changed

11 files changed

+85
-56
lines changed

app/src/main/java/com/chad/baserecyclerviewadapterhelper/activity/dragswipe/DefaultDragAndSwipeActivity.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ import android.graphics.Color
66
import android.os.Build
77
import android.os.Bundle
88
import android.util.Log
9+
import androidx.recyclerview.widget.GridLayoutManager
910
import androidx.recyclerview.widget.ItemTouchHelper
10-
import androidx.recyclerview.widget.LinearLayoutManager
1111
import androidx.recyclerview.widget.RecyclerView
1212
import com.chad.baserecyclerviewadapterhelper.activity.dragswipe.adapter.DragAndSwipeAdapter
1313
import com.chad.baserecyclerviewadapterhelper.base.BaseViewBindingActivity
1414
import com.chad.baserecyclerviewadapterhelper.databinding.ActivityUniversalRecyclerBinding
1515
import com.chad.baserecyclerviewadapterhelper.utils.Tips
16-
import com.chad.baserecyclerviewadapterhelper.utils.VibratorUtils.vibrate
16+
import com.chad.baserecyclerviewadapterhelper.utils.vibrate
1717
import com.chad.library.adapter.base.dragswipe.QuickDragAndSwipe
1818
import com.chad.library.adapter.base.dragswipe.listener.OnItemDragListener
1919
import com.chad.library.adapter.base.dragswipe.listener.OnItemSwipeListener
@@ -28,7 +28,8 @@ class DefaultDragAndSwipeActivity : BaseViewBindingActivity<ActivityUniversalRec
2828
private val mAdapter: DragAndSwipeAdapter = DragAndSwipeAdapter()
2929

3030
private val quickDragAndSwipe = QuickDragAndSwipe()
31-
.setDragMoveFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN)
31+
.setDragMoveFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN or
32+
ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
3233
.setSwipeMoveFlags(ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
3334

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

42-
viewBinding.rv.layoutManager = LinearLayoutManager(this)
43+
viewBinding.rv.layoutManager = GridLayoutManager(this,3)
4344
viewBinding.rv.adapter = mAdapter
4445

4546
val mData = generateData(50)
@@ -48,7 +49,7 @@ class DefaultDragAndSwipeActivity : BaseViewBindingActivity<ActivityUniversalRec
4849
// 拖拽监听
4950
val listener: OnItemDragListener = object : OnItemDragListener {
5051
override fun onItemDragStart(viewHolder: RecyclerView.ViewHolder?, pos: Int) {
51-
vibrate(applicationContext)
52+
vibrate()
5253
Log.d(TAG, "drag start")
5354
val holder = viewHolder as QuickViewHolder? ?: return
5455
// 开始时,item背景色变化,demo这里使用了一个动画渐变,使得自然
@@ -94,6 +95,14 @@ class DefaultDragAndSwipeActivity : BaseViewBindingActivity<ActivityUniversalRec
9495
v.duration = 300
9596
v.start()
9697
}
98+
99+
mAdapter.items.forEach {
100+
Log.d(
101+
TAG,
102+
"-------->> w 顺序 ${it} "
103+
)
104+
}
105+
97106
}
98107
}
99108
val swipeListener: OnItemSwipeListener = object : OnItemSwipeListener {

app/src/main/java/com/chad/baserecyclerviewadapterhelper/activity/dragswipe/DragAndSwipeDifferActivity.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import com.chad.baserecyclerviewadapterhelper.activity.dragswipe.adapter.DiffDra
1313
import com.chad.baserecyclerviewadapterhelper.base.BaseViewBindingActivity
1414
import com.chad.baserecyclerviewadapterhelper.data.DataServer
1515
import com.chad.baserecyclerviewadapterhelper.databinding.ActivityUniversalRecyclerBinding
16-
import com.chad.baserecyclerviewadapterhelper.utils.VibratorUtils.vibrate
16+
import com.chad.baserecyclerviewadapterhelper.utils.vibrate
1717
import com.chad.library.adapter.base.dragswipe.QuickDragAndSwipe
1818
import com.chad.library.adapter.base.dragswipe.listener.DragAndSwipeDataCallback
1919
import com.chad.library.adapter.base.dragswipe.listener.OnItemDragListener
@@ -30,7 +30,7 @@ class DragAndSwipeDifferActivity : BaseViewBindingActivity<ActivityUniversalRecy
3030

3131
private var mAdapter: DiffDragAndSwipeAdapter = DiffDragAndSwipeAdapter()
3232

33-
var quickDragAndSwipe = QuickDragAndSwipe()
33+
private var quickDragAndSwipe = QuickDragAndSwipe()
3434
.setDragMoveFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN)
3535
.setSwipeMoveFlags(ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
3636

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

139139
quickDragAndSwipe.attachToRecyclerView(viewBinding.rv)
140140
.setDataCallback(object : DragAndSwipeDataCallback {
141-
override fun dataSwap(fromPosition: Int, toPosition: Int) {
141+
override fun dataMove(fromPosition: Int, toPosition: Int) {
142142
mAdapter.swap(fromPosition, toPosition)
143143
}
144144

app/src/main/java/com/chad/baserecyclerviewadapterhelper/activity/dragswipe/HeaderDragAndSwipeActivity.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ import com.chad.baserecyclerviewadapterhelper.activity.dragswipe.adapter.HeaderD
1111
import com.chad.baserecyclerviewadapterhelper.activity.home.adapter.HomeTopHeaderAdapter
1212
import com.chad.baserecyclerviewadapterhelper.base.BaseViewBindingActivity
1313
import com.chad.baserecyclerviewadapterhelper.databinding.ActivityUniversalRecyclerBinding
14-
import com.chad.baserecyclerviewadapterhelper.utils.VibratorUtils.vibrate
14+
import com.chad.baserecyclerviewadapterhelper.utils.vibrate
1515
import com.chad.library.adapter.base.QuickAdapterHelper
1616
import com.chad.library.adapter.base.dragswipe.setItemDragListener
1717
import com.chad.library.adapter.base.dragswipe.setItemSwipeListener
1818
import com.chad.library.adapter.base.loadState.LoadState.NotLoading
1919
import com.chad.library.adapter.base.loadState.trailing.TrailingLoadStateAdapter
2020
import com.chad.library.adapter.base.viewholder.QuickViewHolder
21-
import kotlinx.coroutines.*
21+
import kotlinx.coroutines.Dispatchers
22+
import kotlinx.coroutines.GlobalScope
23+
import kotlinx.coroutines.delay
24+
import kotlinx.coroutines.launch
25+
import kotlinx.coroutines.withContext
2226

2327
/**
2428
* 带头部局以及带加载下一页的,拖拽demo
@@ -83,7 +87,7 @@ class HeaderDragAndSwipeActivity : BaseViewBindingActivity<ActivityUniversalRecy
8387
.setItemDragListener(
8488
onItemDragStart = { viewHolder, pos ->
8589
Log.d(TAG, "drag start")
86-
vibrate(applicationContext)
90+
vibrate()
8791
val holder = viewHolder as QuickViewHolder
8892
// 开始时,item背景色变化,demo这里使用了一个动画渐变,使得自然
8993
val startColor = Color.WHITE

app/src/main/java/com/chad/baserecyclerviewadapterhelper/activity/dragswipe/ManualDragAndSwipeUseActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import com.chad.baserecyclerviewadapterhelper.base.BaseViewBindingActivity;
1717
import com.chad.baserecyclerviewadapterhelper.databinding.ActivityUniversalRecyclerBinding;
1818
import com.chad.baserecyclerviewadapterhelper.utils.Tips;
19-
import com.chad.baserecyclerviewadapterhelper.utils.VibratorUtils;
19+
import com.chad.baserecyclerviewadapterhelper.utils.VibratorUtilsKt;
2020
import com.chad.library.adapter.base.QuickAdapterHelper;
2121
import com.chad.library.adapter.base.dragswipe.QuickDragAndSwipe;
2222
import com.chad.library.adapter.base.dragswipe.listener.OnItemDragListener;
@@ -63,7 +63,7 @@ protected void onCreate(Bundle savedInstanceState) {
6363
OnItemDragListener listener = new OnItemDragListener() {
6464
@Override
6565
public void onItemDragStart(@Nullable RecyclerView.ViewHolder viewHolder, int pos) {
66-
VibratorUtils.INSTANCE.vibrate(getApplicationContext());
66+
VibratorUtilsKt.vibrate(getApplicationContext());
6767
Log.d(TAG, "drag start");
6868
final QuickViewHolder holder = ((QuickViewHolder) viewHolder);
6969
if (holder == null) return;

app/src/main/java/com/chad/baserecyclerviewadapterhelper/activity/dragswipe/adapter/DragAndSwipeAdapter.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,18 @@ protected QuickViewHolder onCreateViewHolder(@NonNull Context context, @NonNull
2222
@Override
2323
protected void onBindViewHolder(@NonNull QuickViewHolder holder, int position, String item) {
2424
switch (holder.getLayoutPosition() % 3) {
25-
case 0:
26-
holder.setImageResource(R.id.iv_head, R.mipmap.head_img0);
27-
break;
28-
case 1:
29-
holder.setImageResource(R.id.iv_head, R.mipmap.head_img1);
30-
break;
31-
case 2:
32-
holder.setImageResource(R.id.iv_head, R.mipmap.head_img2);
33-
break;
34-
default:
35-
break;
25+
case 0 -> holder.setImageResource(R.id.iv_head, R.mipmap.head_img0);
26+
case 1 -> holder.setImageResource(R.id.iv_head, R.mipmap.head_img1);
27+
case 2 -> holder.setImageResource(R.id.iv_head, R.mipmap.head_img2);
28+
default -> {
29+
}
3630
}
3731
holder.setText(R.id.tv, item);
3832
}
3933

4034
@Override
41-
public void dataSwap(int fromPosition, int toPosition) {
42-
swap(fromPosition, toPosition);
35+
public void dataMove(int fromPosition, int toPosition) {
36+
move(fromPosition, toPosition);
4337
}
4438

4539
@Override

app/src/main/java/com/chad/baserecyclerviewadapterhelper/activity/dragswipe/adapter/HeaderDragAndSwipeAdapter.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ open class HeaderDragAndSwipeAdapter : BaseQuickAdapter<String, QuickViewHolder>
3131
holder.setText(R.id.tv, item)
3232
}
3333

34-
override fun dataSwap(fromPosition: Int, toPosition: Int) {
35-
swap(fromPosition, toPosition)
34+
override fun dataMove(fromPosition: Int, toPosition: Int) {
35+
move(fromPosition, toPosition)
3636
}
3737

3838
override fun dataRemoveAt(position: Int) {

app/src/main/java/com/chad/baserecyclerviewadapterhelper/utils/VibratorUtils.kt

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,21 @@ import android.os.Vibrator
88
import android.os.VibratorManager
99

1010
/**
11-
* 震动类
11+
* 震动
1212
*/
13-
object VibratorUtils {
14-
/**
15-
* * 轻微的震动提醒
16-
*/
17-
fun vibrate(context: Context) {
18-
if (Build.VERSION.SDK_INT >= 31) {
19-
// android 12 及以上使用新的 VibratorManager,创建 EFFECT_TICK 轻微震动(需要线性震动马达硬件支持)
20-
val manager: VibratorManager =
21-
context.getSystemService<VibratorManager>(VibratorManager::class.java)
22-
manager.defaultVibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK))
23-
} else if (Build.VERSION.SDK_INT >= 29) {
24-
// android 10 及以上使用原 Vibrator,创建 EFFECT_TICK 轻微震动(需要线性震动马达硬件支持)
25-
val vib = context.getSystemService<Vibrator>(Vibrator::class.java) as Vibrator
26-
vib.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK))
27-
} else {
28-
// 10 以下的系统,没有系统 API 驱动线性震动马达,只能创建普通震动
29-
val vib = context.getSystemService(Service.VIBRATOR_SERVICE) as Vibrator //震动70毫秒
30-
vib.vibrate(70)
31-
}
13+
fun Context.vibrate() {
14+
if (Build.VERSION.SDK_INT >= 31) {
15+
// android 12 及以上使用新的 VibratorManager,创建 EFFECT_TICK 轻微震动(需要线性震动马达硬件支持)
16+
val manager: VibratorManager =
17+
getSystemService(VibratorManager::class.java)
18+
manager.defaultVibrator.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK))
19+
} else if (Build.VERSION.SDK_INT >= 29) {
20+
// android 10 及以上使用原 Vibrator,创建 EFFECT_TICK 轻微震动(需要线性震动马达硬件支持)
21+
val vib = getSystemService(Vibrator::class.java) as Vibrator
22+
vib.vibrate(VibrationEffect.createPredefined(VibrationEffect.EFFECT_TICK))
23+
} else {
24+
// 10 以下的系统,没有系统 API 驱动线性震动马达,只能创建普通震动
25+
val vib = getSystemService(Service.VIBRATOR_SERVICE) as Vibrator //震动70毫秒
26+
vib.vibrate(70)
3227
}
3328
}

library/src/main/java/com/chad/library/adapter/base/BaseDifferAdapter.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,21 @@ abstract class BaseDifferAdapter<T, VH : RecyclerView.ViewHolder>(
152152
}
153153

154154
override fun swap(fromPosition: Int, toPosition: Int) {
155-
val size = items.size
156-
if (fromPosition in 0 until size || toPosition in 0 until size) {
155+
if (fromPosition in items.indices || toPosition in items.indices) {
157156
items.toMutableList().also {
158157
Collections.swap(it, fromPosition, toPosition)
159158
submitList(it)
160159
}
161160
}
162161
}
162+
163+
override fun move(fromPosition: Int, toPosition: Int) {
164+
if (fromPosition in items.indices || toPosition in items.indices) {
165+
items.toMutableList().also {
166+
val e = it.removeAt(fromPosition)
167+
it.add(toPosition, e)
168+
submitList(it)
169+
}
170+
}
171+
}
163172
}

library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,15 +623,30 @@ abstract class BaseQuickAdapter<T, VH : RecyclerView.ViewHolder>(
623623

624624
/**
625625
* Item swap
626-
* 数据位置交换
626+
* 数据位置交换。这里单纯的只是两个数据交换位置。(注意⚠️,这里移动后的数据顺序与 [move] 不同)
627627
*
628628
* @param fromPosition
629629
* @param toPosition
630630
*/
631631
open fun swap(fromPosition: Int, toPosition: Int) {
632-
val size = items.size
633-
if (fromPosition in 0 until size && toPosition in 0 until size) {
632+
if (fromPosition in items.indices && toPosition in items.indices) {
634633
Collections.swap(items, fromPosition, toPosition)
634+
notifyItemChanged(fromPosition)
635+
notifyItemChanged(toPosition)
636+
}
637+
}
638+
639+
/**
640+
* Move Item
641+
* item 位置的移动。(注意⚠️,这里移动后的数据顺序与 [swap] 不同)
642+
*
643+
* @param fromPosition
644+
* @param toPosition
645+
*/
646+
open fun move(fromPosition: Int, toPosition: Int) {
647+
if (fromPosition in items.indices && toPosition in items.indices) {
648+
val e = mutableItems.removeAt(fromPosition)
649+
mutableItems.add(toPosition, e)
635650
notifyItemMoved(fromPosition, toPosition)
636651
}
637652
}

library/src/main/java/com/chad/library/adapter/base/dragswipe/QuickDragAndSwipe.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ open class QuickDragAndSwipe : ItemTouchHelper.Callback() {
190190
if (fromPosition == RecyclerView.NO_POSITION || toPosition == RecyclerView.NO_POSITION) return
191191

192192
// 进行位置的切换
193-
_dataCallback?.dataSwap(fromPosition, toPosition)
193+
_dataCallback?.dataMove(fromPosition, toPosition)
194194
mOnItemDragListener?.onItemDragMoving(viewHolder, fromPosition, target, toPosition)
195195
}
196196

0 commit comments

Comments
 (0)