在不同模块中使用Android数据绑定 [英] Android databinding using in different modules

查看:46
本文介绍了在不同模块中使用Android数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有三个模块,模块A,B,C.模块A编译模块B,模块B编译模块C.模块C中有一个layout(layout_c.xml).然后我在模块A的中使用layout_c.xml布局(layout_a.xml).

Now I have three modules,module A ,B,C.module A compile module B,module B compile module C.There is a layout(layout_c.xml) in module C.Then I using layout_c.xml in module A's layout(layout_a.xml).

  1. 有layout_c.xml`

  1. there is layout_c.xml `

</variable>

<variable
    name="handler"
    type="xxxxxx">

</variable>

<RelativeLayout
    ......
</RelativeLayout>

`

有layout_a.xml

there is layout_a.xml

< includeandroid:id ="@ + id/layout_c"layout ="@ layout/layout_c"/>

问题:IDE认为bindingA.layoutC返回一个视图而不是数据绑定.模块C具有BR类和所有数据绑定类.但是模块A没有,所以我该怎么办?

question:IDE think bindingA.layoutC return a view not a databinding.And module C has BR class and all databinding class.But module A doesn't have.So,what should I do?

LayoutABinding bindingA = DataBindingUtil.setContentView(this,R.layout.layout_a);newTitleBarViewModel.setDataBinding(bindingA.layoutC);

推荐答案

为了使数据绑定能够跨多个模块工作-就我而言-我必须确保每个Android Studio模块(库,电话/平板电脑,等等)在其相应的 build.gradle 中启用了数据绑定(不仅仅用于库.gradle文件,因为那还不够)

In order to get data binding to work across multiple modules - in my case - I had to make sure that each Android Studio module (library, phone/tablet, etc) has data binding enabled in its corresponding build.gradle (not just for the library .gradle file, as that was not enough),

像这样:

android {
        ...

    defaultConfig {
        ...
    }
    buildTypes {
        ...
    }

    // Looks like this needs to be set in the app module that uses the lib
    // The lib needs it for the layout binding code there
    dataBinding {
        enabled = true
    }

}


应用解决方案的项目结构:


The project structure where solution is applied :

  1. 有一个库模块,和多个应用程序模块" (又名,可以运行的适用于手机或平板电脑的Android Studio模块)使用同一项目中该库中的活动/布局"

  1. There is a library module, and multiple "app modules" (aka, Android Studio module for Phone or Tablet that can be run) that use Activities/layouts from that library in the same project

库项目在/res/layout下具有.xml,它依赖于数据绑定,如下所示:

The library project has .xml under /res/layout that relies on data binding, like so:

<TextView
    android:id="@+id/display_name_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="@={user.displayName}"
    android:textSize="20sp"/>


这个问题很难解决.特别是在我有多个模块的情况下.在为正在使用的主模块应用此解决方案后,我仍然继续遇到构建错误.最终,我在所有错误输出中注意到还有另外一个模块-我没有使用过-该模块也具有该库作为依赖项,但是缺少了 build.gradle 中的数据绑定启用.最终确定并解决了这些问题后,构建工作正常.我很高兴发现这一点,因为当您可以通过多个模块之间的数据绑定重用布局时,它使事情变得更好了

This one was a bit tricky to resolve. Especially in my case where I have multiple modules. After I applied this solution for the main module I was working with, I still continued getting build errors. Finally I noticed in all the error output that there was 1 other module - which I hadn't been working with - that also had the library as dependency , but was missing the data binding enable in build.gradle. When finally that was identified and addressed, the builds are working fine. I'm glad to have found this as it makes things a lot nicer when you can reuse layouts with data binding across multiple modules

这篇关于在不同模块中使用Android数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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