[英]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
沒有觸發嗎?
請同時確保您已完成此步驟。
fontawesome-webfront
}" 更改為 app:font="@{ fontawesome-webfront
}" 。我認為您錯過了在活動中將您的視圖與 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.