Android数据绑定到自定义视图

问题描述 投票:0回答:1

我要做的是将实时数据传递到自定义视图

我想做什么:

<com.mastherhealth.masterbilling.views.DashboardCard
    android:layout_height="@dimen/dashboard_card_size"
    android:layout_width="@dimen/dashboard_card_size"
    android:layout_marginTop="@dimen/dashboard_card_margin"
    android:layout_marginBottom="@dimen/dashboard_card_margin"
    android:layout_marginStart="@dimen/dashboard_card_center_margin"
    android:layout_marginEnd="@dimen/dashboard_card_center_margin"
    app:layout_constraintTop_toBottomOf="@+id/dashboard_doctor_spinner"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:cardCornerRadius="10dp"
    app:cardBackgroundColor="@color/colorPrimaryDark"
    app:name="Saved"
    app:number="@{`` + viewModel.savedClaimsLiveData}"/>

viewModel.saveClaimsLiveData是一个LiveData,我将从我的存储库中获取。

我的attrs.xml:

<resources>
    <declare-styleable name="DashboardCard">
        <attr name="name" format="string"/>
        <attr name="fontAwesome" format="string"/>
        <attr name="number" format="string"/>
    </declare-styleable>
</resources> 

我有一个扩展CardView的DashboardCard类,按我希望的方式设置名称和编号。

我现在正在接受

****/ data binding error ****msg:Cannot find the setter for attribute 'app:number' with parameter type java.lang.String on com.mastherhealth.masterbilling.views.DashboardCard.
android android-view android-livedata
1个回答
0
投票

您需要为自定义属性添加BindingAdapter

@BindingAdapter("number")
fun bindNumber(view: View, number: String) {
    // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.