From 848fdb6a6d7a048272bcfd364c6c51c710dd4a45 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 22 Mar 2020 19:40:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/base/adapter/CommonRecyclerAdapter.kt | 5 ++--- .../app/ui/book/arrange/ArrangeBookAdapter.kt | 14 +++++++++++--- app/src/main/res/menu/change_cover.xml | 2 +- app/src/main/res/menu/change_source.xml | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) 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 @@