Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用viewbind时,如何在使用xml布局的自定义ViewGroup中使用呢 #26

Closed
jqorz opened this issue May 8, 2021 · 4 comments
Closed
Labels
bug Something isn't working

Comments

@jqorz
Copy link

jqorz commented May 8, 2021

自定义ViewGroup布局名称layout_empty_tip,根标签使用了<merge>
声明了private val binding: LayoutEmptyTipBinding by viewbind()
但是运行报错了

java.lang.NoSuchMethodException: inflate [class android.view.LayoutInflater]
at java.lang.Class.getMethod(Class.java:2068)
at java.lang.Class.getMethod(Class.java:1690)
at com.hi.dhl.binding.ReflectExtKt.inflateMethod(ReflectExt.kt:17)
at com.hi.dhl.binding.viewbind.ViewGroupViewBinding.(ViewGroupViewBinding.kt:24)

看了下应该是反射没找到inflate方法

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

但是不知道怎么解决

@hi-dhl
Copy link
Owner

hi-dhl commented May 8, 2021

  1. 检查一下有没有添加混淆
  2. 我已经添加了,在 ViewGroup 中使用 <merge> ,示例如下所示

LayoutMergeItemBinding.bind(root)
.mergeTvTitle.setText("在 ViewGroup 中使用 merge 标签")
}

@jqorz
Copy link
Author

jqorz commented May 8, 2021

  1. 检查一下有没有添加混淆
  2. 我已经添加了,在 ViewGroup 中使用 <merge> ,示例如下所示

LayoutMergeItemBinding.bind(root)
.mergeTvTitle.setText("在 ViewGroup 中使用 merge 标签")
}

你好,demo和我描述的情况不太一致,我的自定义View的根布局就是merge标签,这样我的merge的Binding没有可以绑定的对象

@hi-dhl
Copy link
Owner

hi-dhl commented May 9, 2021

已经修复,请升级到 1.1.3 的版本,即可

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

详细的 Demo 示例如下所示。

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

@hi-dhl hi-dhl added the bug Something isn't working label May 9, 2021
@jqorz jqorz closed this as completed May 9, 2021
@jqorz
Copy link
Author

jqorz commented May 9, 2021

已经修复,请升级到 1.1.3 的版本,即可

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

详细的 Demo 示例如下所示。

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

感谢作者

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants