diff --git a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/list/DialogListAdapter.kt b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/list/DialogListAdapter.kt index b3f75670..b184e439 100644 --- a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/list/DialogListAdapter.kt +++ b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/list/DialogListAdapter.kt @@ -1,6 +1,7 @@ package com.trendyol.uicomponents.dialogs.list import android.view.ViewGroup +import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView import com.trendyol.dialog.R import com.trendyol.dialog.databinding.ItemListBinding @@ -10,28 +11,20 @@ internal class DialogListAdapter( private val showItemsAsHtml: Boolean, private val selectedItemDrawable: Int?, private val selectedTextColor: Int? -) : RecyclerView.Adapter() { - - private var items: List> = emptyList() +) : ListAdapter, DialogListAdapter.ItemViewHolder>(ListItemDiffCallback()) { var onItemSelectedListener: ((Int) -> Unit)? = null fun setItems(list: List>) { - items = items.toMutableList().apply { - clear() - addAll(list) - } - notifyDataSetChanged() + submitList(list.toMutableList()) } override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { - holder.bind(items[position]) + holder.bind(getItem(position)) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder = ItemViewHolder(parent.inflate(R.layout.item_list, false)) - override fun getItemCount(): Int = items.size - inner class ItemViewHolder( private val binding: ItemListBinding ) : RecyclerView.ViewHolder(binding.root) { @@ -55,4 +48,4 @@ internal class DialogListAdapter( binding.executePendingBindings() } } -} +} \ No newline at end of file diff --git a/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/list/ListItemDiffCallback.kt b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/list/ListItemDiffCallback.kt new file mode 100644 index 00000000..bd078dd1 --- /dev/null +++ b/libraries/dialogs/src/main/java/com/trendyol/uicomponents/dialogs/list/ListItemDiffCallback.kt @@ -0,0 +1,19 @@ +package com.trendyol.uicomponents.dialogs.list + +import androidx.recyclerview.widget.DiffUtil + +class ListItemDiffCallback: DiffUtil.ItemCallback>() { + override fun areContentsTheSame( + oldItem: Pair, + newItem: Pair + ): Boolean { + return oldItem.first == newItem.first && oldItem.second.toString() == newItem.second.toString() + } + + override fun areItemsTheSame( + oldItem: Pair, + newItem: Pair + ): Boolean { + return oldItem.second == newItem.second + } +} \ No newline at end of file diff --git a/libraries/dialogs/src/main/res/layout/item_list.xml b/libraries/dialogs/src/main/res/layout/item_list.xml index bc2f7dae..d69536f1 100644 --- a/libraries/dialogs/src/main/res/layout/item_list.xml +++ b/libraries/dialogs/src/main/res/layout/item_list.xml @@ -13,6 +13,7 @@