[英]Android data binding include layout from different module
我的項目中有多個模塊,假設模塊應用程序和模塊A充當模塊應用程序的庫。 我使用數據綁定並通過添加工作正常
dataBinding { enabled = true }
在每個模塊build.gradle中。
當我使用標簽包含模塊A的布局時發生問題。 當我嘗試訪問包含布局的數據綁定時,它返回View對象而不是ViewDataBinding
但是,當我試圖繼續訪問包含布局中的id時,即使IDE顯示錯誤,編譯也能正常工作。 我嘗試過重建項目,使緩存無效以及其他所有內容。
我肯定已經遵循規則如何實現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>
很可能是構建訂單問題。 您可能需要確保在父模塊嘗試訪問其內容之前構建模塊A.
您在綁定中看到的是您指定聯系的ID。 這很好,因為它找到了您嘗試訪問的布局。 接下來,它將查找聯系人的綁定文件。 應該是基於您的命名約定的IncludedLayoutBinding。 該文件將嵌套的textViews作為tv_text。
現在,如果你沒有看到tv_text,那是因為從未為子模塊創建IncludedLayoutBinding。
您確定您的子模塊已正確啟用數據綁定,並且您確定在嘗試訪問它的值之前已經構建了數據綁定。
您可以檢查childModule / build / source / dataBinding文件夾,以確保您看到創建的layoutBinding類。 我猜它現在不存在了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.