From 7dca987d8b56fa1dc34636f9d59653e4e99e75a8 Mon Sep 17 00:00:00 2001 From: Drakeet Date: Sun, 20 Jun 2021 17:37:30 +0800 Subject: [PATCH] Add view.*Position & holder extensions --- .../com/drakeet/multitype/ViewDelegate.kt | 20 +++++++++++++++++++ library/src/main/res/values/ids.xml | 4 ++++ .../multitype/sample/normal/RichView.kt | 1 + .../sample/normal/RichViewDelegate.kt | 10 ++++++++-- 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 library/src/main/res/values/ids.xml diff --git a/library/src/main/kotlin/com/drakeet/multitype/ViewDelegate.kt b/library/src/main/kotlin/com/drakeet/multitype/ViewDelegate.kt index a5b69ce6..03c26002 100644 --- a/library/src/main/kotlin/com/drakeet/multitype/ViewDelegate.kt +++ b/library/src/main/kotlin/com/drakeet/multitype/ViewDelegate.kt @@ -12,6 +12,16 @@ import androidx.recyclerview.widget.RecyclerView */ abstract class ViewDelegate : ItemViewDelegate>() { + protected val View.holder: Holder 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) @@ -23,9 +33,19 @@ abstract class ViewDelegate : ItemViewDelegate, 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? { + @Suppress("UNCHECKED_CAST") + return view.getTag(R.id.tagViewHolder) as? Holder + } + override fun onCreateViewHolder(context: Context, parent: ViewGroup): Holder { return Holder(onCreateView(context, parent)) } diff --git a/library/src/main/res/values/ids.xml b/library/src/main/res/values/ids.xml new file mode 100644 index 00000000..a95defbe --- /dev/null +++ b/library/src/main/res/values/ids.xml @@ -0,0 +1,4 @@ + + + + diff --git a/sample/src/main/kotlin/com/drakeet/multitype/sample/normal/RichView.kt b/sample/src/main/kotlin/com/drakeet/multitype/sample/normal/RichView.kt index 04b52485..fac4ca0f 100644 --- a/sample/src/main/kotlin/com/drakeet/multitype/sample/normal/RichView.kt +++ b/sample/src/main/kotlin/com/drakeet/multitype/sample/normal/RichView.kt @@ -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)) } diff --git a/sample/src/main/kotlin/com/drakeet/multitype/sample/normal/RichViewDelegate.kt b/sample/src/main/kotlin/com/drakeet/multitype/sample/normal/RichViewDelegate.kt index 65f19da8..bf36c579 100644 --- a/sample/src/main/kotlin/com/drakeet/multitype/sample/normal/RichViewDelegate.kt +++ b/sample/src/main/kotlin/com/drakeet/multitype/sample/normal/RichViewDelegate.kt @@ -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 @@ -35,9 +36,14 @@ class RichViewDelegate : ViewDelegate() { 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() } }