一、定義
BindingAdapter是什麼?弄懂一個東西必須得深刻理解定義。它是爲了給view設置表達式的值在方法上添加註解的註解關鍵詞;信息量有三點:1、它是一個註解關鍵詞;2、他是用來標記的方法的;3、它的功能是用來設置view的屬性值。它修飾的方法必須用public static 修飾。詳細請看官方文檔。
二、用法
根據定義三個點來寫:
1和2,它是一個標記方法的註解關鍵詞;
使用方法 我是建一個類 ,用來集體存放存放自定義註解使用的方法
使用BindingAdapter我感覺主要是爲了對某個控件進行復雜操作
定義方法1
@BindingAdapter("loadtextView")
public static void loadUrlImage(TextView textView, String s) {
textView.setText(s);
LogUtils.INSTANCE.d("loadtextView的方法執行中");
}
使用方法1
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF333333"
android:textSize="26sp"
app:loadtextView="@{listitem.stockNo}" />
定義方法2//建議使用第二種方法。可以標識哪裏使用
@BindingAdapter("nameSpace:loadtextView")
public static void loadUrlImage(TextView textView, String s) {
textView.setText(s);
LogUtils.INSTANCE.d("loadtextView的方法執行中");
}
使用方法2
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF333333"
android:textSize="26sp"
nameSpace:loadtextView="@{listitem.stockNo}" />
以下是list後運行後的打印日誌