如何在自定义视图上使用视图绑定 [英] How to use View Binding on custom views

查看:92
本文介绍了如何在自定义视图上使用视图绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

View Binding已作为Android Jetpack的一部分发布

View Binding got released as part of Android Jetpack

文档: https://developer.android.com/topic/libraries/view-绑定

我的问题是,如何将视图绑定与自定义视图一起使用.Google文档仅显示了用例显示的活动"和片段.

My question is, how to use view binding with custom views. Google documentation has only show-cased Activity and fragment.

我尝试过,但是什么也没显示.

I tried this, but nothing was shown.

LayoutInflater inflater = LayoutInflater.from(getContext());

然后,我用了这个,但还是没有运气.

And then, I used this one, but again, no luck.

LayoutInflater inflater = (LayoutInflater)
            getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

我想也许我没有针对我的视图使用正确的布局充气机,但不确定.

I guess maybe I don't target the correct layout inflater for my view but not sure.

推荐答案

只需通知根以及是否要附加到根

Just inform the root, and whether you want to attach to it

init { // inflate binding and add as view
    binding = ResultProfileBinding.inflate(LayoutInflater.from(context), this, true)
}

这篇关于如何在自定义视图上使用视图绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆