mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
Moved the PagedSearchCategoriesAdapter to use view binding (#4702)
This commit is contained in:
parent
dcbf57f712
commit
8c573eda26
1 changed files with 22 additions and 27 deletions
|
|
@ -1,43 +1,38 @@
|
||||||
package fr.free.nrw.commons.explore.categories
|
package fr.free.nrw.commons.explore.categories
|
||||||
|
|
||||||
import android.view.View
|
import android.view.LayoutInflater
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import androidx.paging.PagedListAdapter
|
import androidx.paging.PagedListAdapter
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
import androidx.recyclerview.widget.DiffUtil
|
||||||
import fr.free.nrw.commons.R
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import fr.free.nrw.commons.category.CATEGORY_PREFIX
|
import fr.free.nrw.commons.category.CATEGORY_PREFIX
|
||||||
import fr.free.nrw.commons.explore.paging.BaseViewHolder
|
import fr.free.nrw.commons.databinding.ItemRecentSearchesBinding
|
||||||
import fr.free.nrw.commons.explore.paging.inflate
|
|
||||||
import kotlinx.android.synthetic.main.item_recent_searches.*
|
|
||||||
|
|
||||||
|
class PagedSearchCategoriesAdapter(private val onCategoryClicked: (String) -> Unit) :
|
||||||
|
PagedListAdapter<String, CategoryItemViewHolder>(PagedSearchCategoriesDiffUtilCallback) {
|
||||||
|
|
||||||
class PagedSearchCategoriesAdapter(val onCategoryClicked: (String) -> Unit) :
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = CategoryItemViewHolder(
|
||||||
PagedListAdapter<String, CategoryItemViewHolder>(
|
ItemRecentSearchesBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
||||||
object : DiffUtil.ItemCallback<String>() {
|
)
|
||||||
override fun areItemsTheSame(oldItem: String, newItem: String) =
|
|
||||||
oldItem == newItem
|
|
||||||
|
|
||||||
override fun areContentsTheSame(oldItem: String, newItem: String) =
|
|
||||||
oldItem == newItem
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryItemViewHolder {
|
|
||||||
return CategoryItemViewHolder(
|
|
||||||
parent.inflate(R.layout.item_recent_searches),
|
|
||||||
onCategoryClicked
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onBindViewHolder(holder: CategoryItemViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: CategoryItemViewHolder, position: Int) {
|
||||||
holder.bind(getItem(position)!!)
|
holder.bind(getItem(position)!!, onCategoryClicked)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class CategoryItemViewHolder(containerView: View, val onCategoryClicked: (String) -> Unit) :
|
class CategoryItemViewHolder(
|
||||||
BaseViewHolder<String>(containerView) {
|
private val binding: ItemRecentSearchesBinding
|
||||||
|
) : RecyclerView.ViewHolder(binding.root) {
|
||||||
override fun bind(item: String) {
|
fun bind(item: String, onCategoryClicked: (String) -> Unit) = with(binding) {
|
||||||
containerView.setOnClickListener { onCategoryClicked(item) }
|
root.setOnClickListener { onCategoryClicked(item) }
|
||||||
textView1.text = item.substringAfter(CATEGORY_PREFIX)
|
textView1.text = item.substringAfter(CATEGORY_PREFIX)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private object PagedSearchCategoriesDiffUtilCallback : DiffUtil.ItemCallback<String>() {
|
||||||
|
override fun areItemsTheSame(oldItem: String, newItem: String) =
|
||||||
|
oldItem == newItem
|
||||||
|
|
||||||
|
override fun areContentsTheSame(oldItem: String, newItem: String) =
|
||||||
|
oldItem == newItem
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue