aboutsummaryrefslogtreecommitdiff
path: root/fastadapter-viewbinding/src
diff options
context:
space:
mode:
authorAllan Wang <allanwang@google.com>2019-11-13 14:28:03 -0800
committerAllan Wang <allanwang@google.com>2019-11-13 14:28:03 -0800
commitb8ca2b0c07a72149e531a55de0269fba8d5d28b6 (patch)
treed3900b62b90256ebc1a63502127d014525408b6e /fastadapter-viewbinding/src
parent8a0c3769b8960282befbaea7d61a0cb4249f9a98 (diff)
downloadkau-b8ca2b0c07a72149e531a55de0269fba8d5d28b6.tar.gz
kau-b8ca2b0c07a72149e531a55de0269fba8d5d28b6.tar.bz2
kau-b8ca2b0c07a72149e531a55de0269fba8d5d28b6.zip
Add unbind functions
Diffstat (limited to 'fastadapter-viewbinding/src')
-rw-r--r--fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt22
1 files changed, 16 insertions, 6 deletions
diff --git a/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt b/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt
index a475615..543a9ef 100644
--- a/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt
+++ b/fastadapter-viewbinding/src/main/kotlin/ca/allanwang/fastadapter/viewbinding/BindingItem.kt
@@ -19,6 +19,8 @@ import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
+import android.widget.ImageView
+import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import ca.allanwang.kau.fastadapter.viewbinding.R
@@ -40,8 +42,8 @@ interface BindingLayout<Binding : ViewBinding> {
}
abstract class BindingItem<Binding : ViewBinding>(open val data: Any?) :
- AbstractItem<BindingItem.ViewHolder>(),
- BindingLayout<Binding> {
+ AbstractItem<BindingItem.ViewHolder>(),
+ BindingLayout<Binding> {
override val type: Int
get() = layoutRes
@@ -62,6 +64,14 @@ abstract class BindingItem<Binding : ViewBinding>(open val data: Any?) :
abstract fun Binding.bindView(holder: ViewHolder, payloads: MutableList<Any>)
+ protected fun unbind(vararg textViews: TextView) {
+ textViews.forEach { it.text = null }
+ }
+
+ protected fun unbind(vararg imageViews: ImageView) {
+ imageViews.forEach { it.setImageDrawable(null) }
+ }
+
final override fun unbindView(holder: ViewHolder) {
super.unbindView(holder)
val binding = holder.getBinding<Binding>()
@@ -71,7 +81,7 @@ abstract class BindingItem<Binding : ViewBinding>(open val data: Any?) :
open fun Binding.unbindView(holder: ViewHolder) {}
final override fun getViewHolder(v: View): ViewHolder =
- ViewHolder(v, layoutRes)
+ ViewHolder(v, layoutRes)
override fun failedToRecycle(holder: ViewHolder): Boolean {
KL.e { "Failed to recycle" }
@@ -87,7 +97,7 @@ abstract class BindingItem<Binding : ViewBinding>(open val data: Any?) :
override fun hashCode(): Int = data.hashCode()
class ViewHolder(itemView: View, internal val layoutRes: Int) :
- RecyclerView.ViewHolder(itemView) {
+ RecyclerView.ViewHolder(itemView) {
/**
* Retrieves a binding.
@@ -109,7 +119,7 @@ abstract class BindingItem<Binding : ViewBinding>(open val data: Any?) :
}
abstract class BindingClickEventHook<Binding : ViewBinding, Item : BindingItem<Binding>>(val identifier: BindingLayout<Binding>) :
- ClickEventHook<Item>() {
+ ClickEventHook<Item>() {
private fun RecyclerView.ViewHolder.binding(): Binding? {
val holder = this as? BindingItem.ViewHolder ?: return null
@@ -136,7 +146,7 @@ abstract class BindingClickEventHook<Binding : ViewBinding, Item : BindingItem<B
}
open fun Binding.onBindMany(viewHolder: RecyclerView.ViewHolder): List<View>? =
- super.onBindMany(viewHolder)
+ super.onBindMany(viewHolder)
final override fun onClick(v: View, position: Int, fastAdapter: FastAdapter<Item>, item: Item) {
BindingItem.getBinding<Binding>(v).onClick(v, position, fastAdapter, item)