android DataBinding在自定义ViewGroup中的绑定方式之一

本文介绍了在自定义LinearLayout中遇到DataBinding绑定失效的问题,详细解析了使用DataBindingUtil.bind()方法来替代inflate()进行有效绑定的步骤。关键在于给View设置一个与DataBinderMapper.getLayoutId()匹配的tag,以确保正确绑定。同时,提供了找到DataBinderMapper文件的路径提示。
摘要由CSDN通过智能技术生成

问题描述:

    在自定义LinearLayout中使用DataBinding的时候:

        1:无法使用setContentView(Activity activity, int layoutId)

       2:使用inflate(LayoutInflater inflater, int layoutId,@Nullable ViewGroup parent, boolean attachToParent)方法无效。

解决方法:采用:DataBindingUtil.bind()进行绑定。

具体使用:如下,必须在调用bind()之前给view设置一个tag;

注意:此处的tag不能随便乱取,而是要取下图中DataBinderMapper中getLayoutId()方法中与对应布局相匹配的tag值。

TestView1Binding binding;
public TestView1(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view=inflater.inflate(R.layout.test_view_1,this);
        view.setTag("layout/test_view_1_0");
        binding=DataBindingUtil.bind(view);
 
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值