Skip to content

Commit

Permalink
fix: not found inflate(LayoutInflater) in the ViewGroup (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-dhl committed May 9, 2021
1 parent 7adf844 commit f3de58d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencies {
ext.material = "1.2.1"
ext.appcompat = "1.2.0"
ext.ktx = "1.3.2"
ext.remote = true
ext.remote = false

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "androidx.core:core-ktx:${ktx}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ class ViewBindCustomView @JvmOverloads constructor(

lateinit var onDialogClickListener: OnDialogClickListener

val binding: LayoutViewCustomBinding by viewbind()
// 当根布局为 merge 标签,使用此方法进行初始化
val binding: LayoutViewCustomBinding by viewbind(this)

// 当根布局是非 merge 标签,使用此方法进行初始化
// val binding: LayoutViewCustomBinding by viewbind()

init {
with(binding) {
Expand Down
25 changes: 14 additions & 11 deletions app/src/main/res/layout/layout_view_custom.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<merge xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="30dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<include layout="@layout/layout_merge_item" />
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="30dp" />

</LinearLayout>
<include layout="@layout/layout_merge_item" />
</LinearLayout>

</merge>
9 changes: 8 additions & 1 deletion binding/src/main/java/com/hi/dhl/binding/ext/ComponentExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,11 @@ inline fun <reified T : ViewBinding> RecyclerView.ViewHolder.viewbind() =
inline fun <reified T : ViewBinding> ViewGroup.viewbind() = ViewGroupViewBinding(
T::class.java,
LayoutInflater.from(getContext())
)
)

inline fun <reified T : ViewBinding> ViewGroup.viewbind(viewGroup: ViewGroup) =
ViewGroupViewBinding(
T::class.java,
LayoutInflater.from(getContext()),
viewGroup
)
4 changes: 4 additions & 0 deletions binding/src/main/java/com/hi/dhl/binding/ext/ReflectExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.hi.dhl.binding

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

/**
* <pre>
Expand All @@ -16,4 +17,7 @@ const val BIND_NAME = "bind"

fun <T> Class<T>.inflateMethod() = getMethod(INFLATE_NAME, LayoutInflater::class.java)

fun <T> Class<T>.inflateMethodWithViewGroup() =
getMethod(INFLATE_NAME, LayoutInflater::class.java, ViewGroup::class.java)

fun <T> Class<T>.bindMethod() = getMethod(BIND_NAME, View::class.java)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.hi.dhl.binding.inflateMethod
import com.hi.dhl.binding.inflateMethodWithViewGroup
import java.lang.reflect.Method
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty

Expand All @@ -17,20 +19,38 @@ import kotlin.reflect.KProperty

class ViewGroupViewBinding<T : ViewBinding>(
classes: Class<T>,
val inflater: LayoutInflater
val inflater: LayoutInflater,
val viewGroup: ViewGroup? = null
) : ReadOnlyProperty<ViewGroup, T> {

private var viewBinding: T? = null
private val layoutInflater = classes.inflateMethod()
private var layoutInflater: Method

init {
if (viewGroup != null) {
layoutInflater = classes.inflateMethodWithViewGroup()
} else {
layoutInflater = classes.inflateMethod()
}
}

override fun getValue(thisRef: ViewGroup, property: KProperty<*>): T {
return viewBinding?.run {
this

} ?: let {
val bind = layoutInflater.invoke(null, inflater) as T

val bind: T
if (viewGroup != null) {
bind = layoutInflater.invoke(null, inflater, viewGroup) as T
} else {
bind = layoutInflater.invoke(null, inflater) as T
}

bind.apply {
thisRef.addView(bind.root)
if (viewGroup == null) {
thisRef.addView(bind.root)
}
viewBinding = this
}
}
Expand Down

0 comments on commit f3de58d

Please sign in to comment.