Skip to content

Commit

Permalink
Add view.*Position & holder extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
drakeet committed Jun 20, 2021
1 parent eefe2c2 commit 7dca987
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
20 changes: 20 additions & 0 deletions library/src/main/kotlin/com/drakeet/multitype/ViewDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import androidx.recyclerview.widget.RecyclerView
*/
abstract class ViewDelegate<T, V : View> : ItemViewDelegate<T, ViewDelegate.Holder<V>>() {

protected val View.holder: Holder<V> get() {
return holder(this) ?: throw IllegalAccessException("The view holder property can only be called after onCreateView()!")
}

protected val View.layoutPosition: Int get() = holder.layoutPosition

protected val View.absoluteAdapterPosition: Int get() = holder.absoluteAdapterPosition

protected val View.bindingAdapterPosition: Int get() = holder.bindingAdapterPosition

abstract fun onCreateView(context: Context): V

abstract fun onBindView(view: V, item: T)
Expand All @@ -23,9 +33,19 @@ abstract class ViewDelegate<T, V : View> : ItemViewDelegate<T, ViewDelegate.Hold

// Override this function if you need a ViewHolder or positions
open fun onBindView(holder: Holder<V>, view: V, item: T) {
view.setTag(R.id.tagViewHolder, holder)
onBindView(view, item)
}

protected fun getRecyclerLayoutParams(view: View): RecyclerView.LayoutParams {
return (view.layoutParams as RecyclerView.LayoutParams)
}

private fun holder(view: View): Holder<V>? {
@Suppress("UNCHECKED_CAST")
return view.getTag(R.id.tagViewHolder) as? Holder<V>
}

override fun onCreateViewHolder(context: Context, parent: ViewGroup): Holder<V> {
return Holder(onCreateView(context, parent))
}
Expand Down
4 changes: 4 additions & 0 deletions library/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="tagViewHolder" type="id" />
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class RichView(context: Context) : LinearLayout(context) {
}

val textView = AppCompatTextView(context).apply {
gravity = Gravity.CENTER
setTextColor(Color.BLACK)
addView(this, LayoutParams(WRAP_CONTENT, WRAP_CONTENT))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.drakeet.multitype.sample.normal

import android.annotation.SuppressLint
import android.content.Context
import android.view.Gravity
import android.view.ViewGroup
Expand All @@ -35,9 +36,14 @@ class RichViewDelegate : ViewDelegate<RichItem, RichView>() {
return RichView(context).apply { layoutParams = LayoutParams(MATCH_PARENT, WRAP_CONTENT) }
}

@SuppressLint("SetTextI18n")
override fun onBindView(view: RichView, item: RichItem) {
view.imageView.setImageResource(item.imageResId)
view.textView.text = item.text
// Or bind the data in the RichView by calling view.setRichItem(item)
view.textView.text = """
|${item.text}
|layoutPosition: ${view.layoutPosition}
|absoluteAdapterPosition: ${view.absoluteAdapterPosition}
|bindingAdapterPosition: ${view.bindingAdapterPosition}
""".trimMargin()
}
}

0 comments on commit 7dca987

Please sign in to comment.