From e7b6a12d97cfcfee1c47680428731f117eb80e5e Mon Sep 17 00:00:00 2001 From: Naveen Date: Wed, 16 Nov 2022 23:13:56 +0530 Subject: [PATCH] Use recycler view payload for selection states Otherwise the default change animation is shown for the whole item which we really do not want in case of dynamically loaded items (e.g. images loaded using glide) as it can scroll artifacts --- .../adapters/MyRecyclerViewListAdapter.kt | 16 ++++++++++++++-- .../commons/models/RecyclerViewPayloads.kt | 3 +++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 commons/src/main/kotlin/com/simplemobiletools/commons/models/RecyclerViewPayloads.kt diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/MyRecyclerViewListAdapter.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/MyRecyclerViewListAdapter.kt index a2cc34801..1c5e7294c 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/MyRecyclerViewListAdapter.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/MyRecyclerViewListAdapter.kt @@ -5,6 +5,7 @@ import android.view.* import android.widget.ImageView import android.widget.TextView import androidx.appcompat.app.ActionBar +import androidx.core.content.res.ResourcesCompat import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.ListAdapter @@ -13,7 +14,9 @@ import com.simplemobiletools.commons.R import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.interfaces.MyActionModeCallback +import com.simplemobiletools.commons.models.RecyclerSelectionPayload import com.simplemobiletools.commons.views.MyRecyclerView +import kotlin.math.min abstract class MyRecyclerViewListAdapter( val activity: BaseSimpleActivity, @@ -84,7 +87,7 @@ abstract class MyRecyclerViewListAdapter( activity.menuInflater.inflate(getActionMenuId(), menu) val bgColor = if (baseConfig.isUsingSystemTheme) { - resources.getColor(R.color.you_contextual_status_bar_color, activity.theme) + ResourcesCompat.getColor(resources, R.color.you_contextual_status_bar_color, activity.theme) } else { Color.BLACK } @@ -152,9 +155,18 @@ abstract class MyRecyclerViewListAdapter( } } + override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList) { + val any = payloads.firstOrNull() + if (any is RecyclerSelectionPayload) { + holder.itemView.isSelected = any.selected + } else { + onBindViewHolder(holder, position) + } + } + private fun updateTitle() { val selectableItemCount = getSelectableItemCount() - val selectedCount = Math.min(selectedKeys.size, selectableItemCount) + val selectedCount = min(selectedKeys.size, selectableItemCount) val oldTitle = actBarTextView?.text val newTitle = "$selectedCount / $selectableItemCount" if (oldTitle != newTitle) { diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/models/RecyclerViewPayloads.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/models/RecyclerViewPayloads.kt new file mode 100644 index 000000000..08dd297e7 --- /dev/null +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/models/RecyclerViewPayloads.kt @@ -0,0 +1,3 @@ +package com.simplemobiletools.commons.models + +data class RecyclerSelectionPayload(val selected: Boolean)