簡體   English   中英

未調用 Android `@BindingAdapter` setter

[英]Android `@BindingAdapter` setter not being called

這是我的BindingAdapter

public class Bindings{
    @BindingAdapter({"font"})
    public static void setFont(TextView textView, String fontName) {
        textView.setTypeface(FontCache.getInstance(textView.getContext()).get(fontName));
    }
}

*我嘗試了“bind:font”、“android:font”和“app:font”,而不是使用“font”作為注釋參數,並在布局中進行了所有相應的更改,但BindingAdapter仍然不叫

這是我使用BindingAdapter (bind_layout.xml) 的布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <data></data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            font="@{`fontawesome-webfront`}"
            android:text="@string/double_left"/>
    </LinearLayout>
</layout>

*此布局包含在使用DatabindingUtils.setContentView設置的 Activity 布局中

這是布局包含 bind_layout.xml 的活動:

public class ACreateSemester extends AppCompatActivity {
    private List<CreateItemView> mCreateItemViews;
    private LinearLayout mItemContainer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DataBindingUtil.setContentView(this,R.layout.a_create_items);
        mItemContainer = (LinearLayout) findViewById(R.id.item_container);
        mItemContainer.addView(new CreateItemView(this, Item.getDefaultItem()));
    }

}

我在此處引用的三個文件完整列出。

我知道BindingAdapter沒有被調用的方式是因為我在方法和正文中設置了一個斷點,並且從未到達斷點。

知道為什么BindingAdapter沒有觸發嗎?

請同時確保您已完成此步驟。

  1. 在您的應用級別 build.gradle 中應用插件:'kotlin-kapt'。
  2. 將生命周期所有者設置為從您的活動的 onCreate() 方法綁定,例如 binding.lifecycleOwner = this,其中這是您的活動。
  3. 將 xml 中的 font="@{ fontawesome-webfront }" 更改為 app:font="@{ fontawesome-webfront }" 。
  4. 在 xml 布局中向您的按鈕添加一些 Id。

我認為您錯過了在活動中將您的視圖與 viewModel 連接起來。

public class ACreateSemester extends AppCompatActivity {
private List<CreateItemView> mCreateItemViews;
private LinearLayout mItemContainer;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ACreateItemsBinding binding =  DataBindingUtil.setContentView(this,R.layout.a_create_items);
    mItemContainer = (LinearLayout) findViewById(R.id.item_container);
    mItemContainer.addView(new  CreateItemView(this,Item.getDefaultItem()));
    binding.setView(YourViewHere);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM