diff --git a/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt b/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt index f864aa4e8..6a72c911f 100644 --- a/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt +++ b/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt @@ -290,12 +290,11 @@ abstract class CommonRecyclerAdapter(protected val context: Context) : return footerItems?.size() ?: 0 } - fun getItem(position: Int): ITEM? = - if (position in 0 until items.size) items[position] else null + fun getItem(position: Int): ITEM? = items.getOrNull(position) fun getItemByLayoutPosition(position: Int): ITEM? { val pos = position - getHeaderCount() - return if (pos in 0 until items.size) items[pos] else null + return items.getOrNull(pos) } fun getItems(): List = items diff --git a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt index 8ceba8f15..8c36e81df 100644 --- a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt @@ -125,11 +125,19 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) : private var isMoved = false override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { + val srcItem = getItem(srcPosition) + val targetItem = getItem(targetPosition) Collections.swap(getItems(), srcPosition, targetPosition) notifyItemMoved(srcPosition, targetPosition) - if (getItem(srcPosition)?.order == getItem(targetPosition)?.order) { - for ((index, item) in getItems().withIndex()) { - item.order = index + 1 + if (srcItem != null && targetItem != null) { + if (srcItem.order == targetItem.order) { + for ((index, item) in getItems().withIndex()) { + item.order = index + 1 + } + } else { + val pos = srcItem.order + srcItem.order = targetItem.order + targetItem.order = pos } } isMoved = true diff --git a/app/src/main/res/menu/change_cover.xml b/app/src/main/res/menu/change_cover.xml index cce382d12..bf7d0d51b 100644 --- a/app/src/main/res/menu/change_cover.xml +++ b/app/src/main/res/menu/change_cover.xml @@ -6,7 +6,7 @@ diff --git a/app/src/main/res/menu/change_source.xml b/app/src/main/res/menu/change_source.xml index 36f9bee6e..ab2bfee4c 100644 --- a/app/src/main/res/menu/change_source.xml +++ b/app/src/main/res/menu/change_source.xml @@ -14,7 +14,7 @@