【发布时间】:2018-04-16 13:30:09
【问题描述】:
我要上课:
public class Product {
private int id;
private String title;
.............
.............
}
其他类如下:
public class CatalogItem extends Product {
private int price;
private String[] colors;
.............
.............
}
在布局中,我想访问子类和父类的所有属性。
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="catalogItem "
type="com.appdemo.db.entity.CatalogItem" />
</data>
<TextView
android:id="@+id/productIdTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@{((com.appdemo.db.entity.Product) catalogItem).id}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
..................
..................
</layout>
但是在数据绑定的情况下,无法访问父类的属性。访问父类属性的正确方法是什么?
【问题讨论】:
-
您可以轻松地以编程方式(动态)来代替吗?是否可以?在 xml 中设计模式,然后在 Java 中动态添加您的项目。
-
为什么要单独设置各个属性,那Data Binding有什么用,@Fatmajk
标签: android data-binding parent-child