【问题标题】:Android data binding include layout from different moduleAndroid 数据绑定包括来自不同模块的布局
【发布时间】:2018-03-08 09:52:37
【问题描述】:

我的项目中有多个模块,假设模块 app 和模块 A 充当模块 app 的库。我使用数据绑定并通过添加工作正常

dataBinding { enabled = true }

在每个模块 build.gradle.

当我使用标签从模块 A 中包含布局时出现问题。当我尝试访问包含布局的数据绑定时,它返回 View 对象而不是 ViewDataBinding

Android studio autocomplete

但是,当我尝试继续访问包含布局中的 id 时,即使 IDE 显示错误,编译也可以正常工作。我已经尝试过重建项目、使缓存无效和其他一切。

Error on android studio

而且我确信已经遵循了如何实现 DataBinding 的规则。这是模块应用程序的布局:

<layout xmlns:android="http://schemas.android.com/apk/res/android>
    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@color/light_gray"
       android:orientation="vertical">

        <include layout="@layout/included_layout" id="@+id/contact"/>
    </LinearLayout>
</layout>

模块 A 的布局:

<layout xmlns:android="http://schemas.android.com/apk/res/android>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</layout>

【问题讨论】:

  • IDE 显示什么错误?
  • 无法解析符号,基本上是找不到i.stack.imgur.com/afIQX.png
  • 您找到解决方案了吗?
  • 您找到解决方案了吗? (2)

标签: android android-studio data-binding


【解决方案1】:

很可能是构建顺序问题。您可能需要确保在父模块尝试访问它的内容之前构建模块 A。

您在绑定中看到的是您分配给联系人的 ID。这很好,因为它找到了您尝试访问的布局。接下来它会查找联系人的绑定文件。根据您的命名约定,应该类似于 IncludedLayoutBinding 。该文件将在其中包含嵌套的 textViews 作为 tv_text。

现在,如果您没有看到 tv_text,那是因为从未为子模块创建 IncludedLayoutBinding。

您确定您的子模块已正确启用数据绑定,并且您确定它在您尝试访问它的值之前已构建。

您可以检查 childModule/build/source/dataBinding 文件夹以确保您看到您的 layoutBinding 类已创建。我猜它目前不存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    相关资源
    最近更新 更多