diff --git a/app/src/main/java/io/legado/app/help/ItemTouchCallback.kt b/app/src/main/java/io/legado/app/help/ItemTouchCallback.kt index 5454b6e56..0e9a29351 100644 --- a/app/src/main/java/io/legado/app/help/ItemTouchCallback.kt +++ b/app/src/main/java/io/legado/app/help/ItemTouchCallback.kt @@ -104,13 +104,21 @@ class ItemTouchCallback : ItemTouchHelper.Callback() { viewPager?.requestDisallowInterceptTouchEvent(swiping) } + override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + super.clearView(recyclerView, viewHolder) + onItemTouchCallbackListener?.clearView(recyclerView, viewHolder) + } + interface OnItemTouchCallbackListener { + /** * 当某个Item被滑动删除的时候 * * @param adapterPosition item的position */ - fun onSwiped(adapterPosition: Int) + fun onSwiped(adapterPosition: Int) { + + } /** * 当两个Item位置互换的时候被回调 @@ -119,6 +127,13 @@ class ItemTouchCallback : ItemTouchHelper.Callback() { * @param targetPosition 目的地的Item的position * @return 开发者处理了操作应该返回true,开发者没有处理就返回false */ - fun onMove(srcPosition: Int, targetPosition: Int): Boolean + fun onMove(srcPosition: Int, targetPosition: Int): Boolean { + return false + } + + fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + + } + } } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt index e4956c85b..051081e10 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt @@ -15,6 +15,7 @@ import androidx.lifecycle.Observer import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import io.legado.app.App import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder @@ -35,6 +36,8 @@ import kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.dialog_recycler_view.* import kotlinx.android.synthetic.main.item_group_manage.view.* import org.jetbrains.anko.sdk27.listeners.onClick +import java.util.* +import kotlin.collections.ArrayList class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { private lateinit var viewModel: GroupViewModel @@ -175,6 +178,8 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { SimpleRecyclerAdapter(context, R.layout.item_group_manage), ItemTouchCallback.OnItemTouchCallbackListener { + private var isMoved = false + override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList) { with(holder.itemView) { tv_group.text = item.groupName @@ -184,19 +189,20 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { } override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { - val srcItem = getItem(srcPosition) - val targetItem = getItem(targetPosition) - if (srcItem != null && targetItem != null) { - val order = srcItem.order - srcItem.order = targetItem.order - targetItem.order = order - viewModel.upGroup(srcItem, targetItem) - } + Collections.swap(getItems(), srcPosition, targetPosition) + notifyItemMoved(srcPosition, targetPosition) + isMoved = true return true } - override fun onSwiped(adapterPosition: Int) { - + override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + if (isMoved) { + for ((index, item) in getItems().withIndex()) { + item.order = index + 1 + } + viewModel.upGroup(*getItems().toTypedArray()) + } + isMoved = false } } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt index 3541defb4..89c1b4b87 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupSelectDialog.kt @@ -15,6 +15,7 @@ import androidx.fragment.app.FragmentManager import androidx.lifecycle.Observer import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import io.legado.app.App import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder @@ -34,6 +35,7 @@ import kotlinx.android.synthetic.main.dialog_edit_text.view.* import kotlinx.android.synthetic.main.dialog_recycler_view.* import kotlinx.android.synthetic.main.item_group_manage.view.* import org.jetbrains.anko.sdk27.listeners.onClick +import java.util.* class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { @@ -152,6 +154,8 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { SimpleRecyclerAdapter(context, R.layout.item_group_manage), ItemTouchCallback.OnItemTouchCallbackListener { + private var isMoved: Boolean = false + override fun convert(holder: ItemViewHolder, item: BookGroup, payloads: MutableList) { with(holder.itemView) { tv_group.text = item.groupName @@ -165,19 +169,20 @@ class GroupSelectDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { } override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { - val srcItem = getItem(srcPosition) - val targetItem = getItem(targetPosition) - if (srcItem != null && targetItem != null) { - val order = srcItem.order - srcItem.order = targetItem.order - targetItem.order = order - viewModel.upGroup(srcItem, targetItem) - } + Collections.swap(getItems(), srcPosition, targetPosition) + notifyItemMoved(srcPosition, targetPosition) + isMoved = true return true } - override fun onSwiped(adapterPosition: Int) { - + override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + if (isMoved) { + for ((index, item) in getItems().withIndex()) { + item.order = index + 1 + } + viewModel.upGroup(*getItems().toTypedArray()) + } + isMoved = false } } diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt index 3413cdd0e..8e873422b 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceAdapter.kt @@ -5,6 +5,7 @@ import android.os.Bundle import android.view.Menu import android.widget.PopupMenu import androidx.core.os.bundleOf +import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter @@ -13,6 +14,7 @@ import io.legado.app.help.ItemTouchCallback.OnItemTouchCallbackListener import io.legado.app.lib.theme.backgroundColor import kotlinx.android.synthetic.main.item_book_source.view.* import org.jetbrains.anko.sdk27.listeners.onClick +import java.util.* class BookSourceAdapter(context: Context, val callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_book_source), @@ -48,26 +50,6 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : return selection } - override fun onSwiped(adapterPosition: Int) { - - } - - override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { - val srcItem = getItem(srcPosition) - val targetItem = getItem(targetPosition) - if (srcItem != null && targetItem != null) { - if (srcItem.customOrder == targetItem.customOrder) { - callBack.upOrder() - } else { - val srcOrder = srcItem.customOrder - srcItem.customOrder = targetItem.customOrder - targetItem.customOrder = srcOrder - callBack.update(srcItem, targetItem) - } - } - return true - } - override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList) { with(holder.itemView) { val payload = payloads.getOrNull(0) as? Bundle @@ -121,6 +103,35 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : } } } + + } + + override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { + val srcItem = getItem(srcPosition) + val targetItem = getItem(targetPosition) + if (srcItem != null && targetItem != null) { + if (srcItem.customOrder == targetItem.customOrder) { + callBack.upOrder() + } else { + val srcOrder = srcItem.customOrder + srcItem.customOrder = targetItem.customOrder + targetItem.customOrder = srcOrder + movedItems.add(srcItem) + movedItems.add(targetItem) + } + } + Collections.swap(getItems(), srcPosition, targetPosition) + notifyItemMoved(srcPosition, targetPosition) + return true + } + + private val movedItems = hashSetOf() + + override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + if (movedItems.isNotEmpty()) { + callBack.update(*movedItems.toTypedArray()) + movedItems.clear() + } } interface CallBack { diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt index f25c0e523..0a08d2021 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt @@ -5,6 +5,7 @@ import android.os.Bundle import android.view.Menu import android.widget.PopupMenu import androidx.core.os.bundleOf +import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter @@ -13,6 +14,7 @@ import io.legado.app.help.ItemTouchCallback import io.legado.app.lib.theme.backgroundColor import kotlinx.android.synthetic.main.item_replace_rule.view.* import org.jetbrains.anko.sdk27.listeners.onClick +import java.util.* class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : @@ -117,14 +119,22 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : val srcOrder = srcItem.order srcItem.order = targetItem.order targetItem.order = srcOrder - callBack.update(srcItem, targetItem) + movedItems.add(srcItem) + movedItems.add(targetItem) } } + Collections.swap(getItems(), srcPosition, targetPosition) + notifyItemMoved(srcPosition, targetPosition) return true } - override fun onSwiped(adapterPosition: Int) { + private val movedItems = linkedSetOf() + override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + if (movedItems.isNotEmpty()) { + callBack.update(*movedItems.toTypedArray()) + movedItems.clear() + } } interface CallBack { diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt index c54f126d8..9d1f2f1a0 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt @@ -56,7 +56,7 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application fun upOrder() { execute { val rules = App.db.replaceRuleDao().all - for ((index: Int, rule: ReplaceRule) in rules.withIndex()) { + for ((index, rule) in rules.withIndex()) { rule.order = index + 1 } App.db.replaceRuleDao().update(*rules.toTypedArray()) diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt index 708abb904..1ec7d8bbd 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceAdapter.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.rss.source.manage import android.content.Context import android.view.Menu import android.widget.PopupMenu +import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter @@ -11,6 +12,7 @@ import io.legado.app.help.ItemTouchCallback import io.legado.app.lib.theme.backgroundColor import kotlinx.android.synthetic.main.item_rss_source.view.* import org.jetbrains.anko.sdk27.listeners.onClick +import java.util.* class RssSourceAdapter(context: Context, val callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_rss_source), @@ -92,11 +94,6 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) : } } - - override fun onSwiped(adapterPosition: Int) { - - } - override fun onMove(srcPosition: Int, targetPosition: Int): Boolean { val srcItem = getItem(srcPosition) val targetItem = getItem(targetPosition) @@ -107,12 +104,24 @@ class RssSourceAdapter(context: Context, val callBack: CallBack) : val srcOrder = srcItem.customOrder srcItem.customOrder = targetItem.customOrder targetItem.customOrder = srcOrder - callBack.update(srcItem, targetItem) + movedItems.add(srcItem) + movedItems.add(targetItem) } } + Collections.swap(getItems(), srcPosition, targetPosition) + notifyItemMoved(srcPosition, targetPosition) return true } + private val movedItems = hashSetOf() + + override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { + if (movedItems.isNotEmpty()) { + callBack.update(*movedItems.toTypedArray()) + movedItems.clear() + } + } + interface CallBack { fun del(source: RssSource) fun edit(source: RssSource)