Skip to content

Commit

Permalink
Add ripple effect to list items
Browse files Browse the repository at this point in the history
- Start using diffutil to persist ripple effect.
  • Loading branch information
MertNYuksel committed Jan 6, 2020
1 parent 0181535 commit 04c30c2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,28 +11,20 @@ internal class DialogListAdapter(
private val showItemsAsHtml: Boolean,
private val selectedItemDrawable: Int?,
private val selectedTextColor: Int?
) : RecyclerView.Adapter<DialogListAdapter.ItemViewHolder>() {

private var items: List<Pair<Boolean, CharSequence>> = emptyList()
) : ListAdapter<Pair<Boolean, CharSequence>, DialogListAdapter.ItemViewHolder>(ListItemDiffCallback()) {
var onItemSelectedListener: ((Int) -> Unit)? = null

fun setItems(list: List<Pair<Boolean, CharSequence>>) {
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) {
Expand All @@ -55,4 +48,4 @@ internal class DialogListAdapter(
binding.executePendingBindings()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.trendyol.uicomponents.dialogs.list

import androidx.recyclerview.widget.DiffUtil

class ListItemDiffCallback: DiffUtil.ItemCallback<Pair<Boolean, CharSequence>>() {
override fun areContentsTheSame(
oldItem: Pair<Boolean, CharSequence>,
newItem: Pair<Boolean, CharSequence>
): Boolean {
return oldItem.first == newItem.first && oldItem.second.toString() == newItem.second.toString()
}

override fun areItemsTheSame(
oldItem: Pair<Boolean, CharSequence>,
newItem: Pair<Boolean, CharSequence>
): Boolean {
return oldItem.second == newItem.second
}
}
1 change: 1 addition & 0 deletions libraries/dialogs/src/main/res/layout/item_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:padding="@dimen/dialogs_margin_outer">

<androidx.appcompat.widget.AppCompatTextView
Expand Down

0 comments on commit 04c30c2

Please sign in to comment.