From bbc6067a8fd27975418eb71f271fc9021ee1c2be Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 26 Jan 2020 11:28:36 +0800 Subject: [PATCH] up --- .../ui/main/bookshelf/GroupManageDialog.kt | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt index 0d432e437..1022f4ed9 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/GroupManageDialog.kt @@ -12,10 +12,7 @@ import android.widget.EditText import androidx.appcompat.widget.Toolbar import androidx.fragment.app.DialogFragment import androidx.lifecycle.Observer -import androidx.recyclerview.widget.DividerItemDecoration -import androidx.recyclerview.widget.ItemTouchHelper -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.* import io.legado.app.App import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder @@ -79,7 +76,10 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { ) recycler_view.adapter = adapter App.db.bookGroupDao().liveDataAll().observe(viewLifecycleOwner, Observer { - adapter.setItems(it) + val diffResult = + DiffUtil.calculateDiff(GroupDiffCallBack(adapter.getItems(), it)) + adapter.setItems(it, false) + diffResult.dispatchUpdatesTo(adapter) }) val itemTouchCallback = ItemTouchCallback() itemTouchCallback.onItemTouchCallbackListener = adapter @@ -145,6 +145,34 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { }.show().applyTint().requestInputMethod() } + private class GroupDiffCallBack( + private val oldItems: List, + private val newItems: List + ) : + DiffUtil.Callback() { + override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { + val oldItem = oldItems[oldItemPosition] + val newItem = newItems[newItemPosition] + return oldItem.groupId == newItem.groupId + } + + override fun getOldListSize(): Int { + return oldItems.size + } + + override fun getNewListSize(): Int { + return newItems.size + } + + override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean { + val oldItem = oldItems[oldItemPosition] + val newItem = newItems[newItemPosition] + return oldItem.groupName == newItem.groupName + && oldItem.order == newItem.order + } + + } + private inner class GroupAdapter(context: Context) : SimpleRecyclerAdapter(context, R.layout.item_group_manage), ItemTouchCallback.OnItemTouchCallbackListener {