雖然沒有開通專欄以前已經在挖金投稿過了這篇文章,可是我打算寫一個關於android data binding庫的一系列的文章,爲了完整性,我仍是在這裏從新發布一遍。若是以前已經看過這篇android data binding 實踐之:data binding 註解,那麼能夠忽略下面的內容,若是你喜歡的話能夠收藏也能夠點贊哦!html
其實在android data binding這個庫裏面核心的內容我以爲就是下面幾個:java
上面這幾個核心內容我都會在接下來的文章中討論,幾天先把幾個關鍵的註解的使用進行簡單的分析。喜歡的能夠點贊能夠收藏哦!android
data binding的意義主要數據的變更能夠自動觸發UI界面的刷新。可是若是咱們使用的是傳統的java bean對象的時候,是沒有辦法實現「數據變動觸發ui界面」的目的的。而 Bindable 註解就是幫助咱們完成這個任務的。express
若是咱們要實現「數據變動觸發ui界面」的話,途徑主要有兩個:數組
/** * The Bindable annotation should be applied to any getter accessor method of an * {@link Observable} class. Bindable will generate a field in the BR class to identify * the field that has changed. * * @see OnPropertyChangedCallback#onPropertyChanged(Observable, int) */
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME) // this is necessary for java analyzer to work
public @interface Bindable {
}複製代碼
根據上面代碼中的註釋咱們能夠知道,Bindable 是對繼承了 Observable 的類當中的 getter 方法進行註解。而且當咱們使用了這個註解的時候,databinding library 會在 BR 這個類當中生成一個屬性,用以標識發生了變化的屬性fieldapp
public class PrivateUser extends BaseObservable{
private String fistName;
private String lastName;
public PrivateUser(String fistName, String lastName) {
this.age = age;
this.fistName = fistName;
this.lastName = lastName;
}
@Bindable
public String getFistName() {
return fistName;
}
public void setFistName(String fistName) {
this.fistName = fistName;
notifyPropertyChanged(BR.fistName);
}
@Bindable
public String getLastName() {
return lastName;
}
public void setLasetName(String lasetName) {
this.lastName = lastName;
notifyPropertyChanged(BR.lastName);
}
}複製代碼
當咱們使用data binding的時候,data-binding library會盡量的找到給view對應的屬性(Attribute)進行賦值的方法,一般這個方法的名稱就是set${Attribute}。這時候屬性前面的命名空間會被忽略,而只關注屬性的名稱。好比咱們數據綁定了TextView的 android:text 屬性,那麼data-binding library會去尋找 setText(String) 這樣的一個方法。框架
BindingAdapter 註解的方法可以控制給view賦值的操做過程,也就是說能夠實現自定義setter的實現。對於那些不具有對應setter方法的屬性(好比咱們要綁定一個 android:paddingLeft 的屬性的時候,咱們卻只有 setPadding(left, top, right, bottom) 這樣的一個setter方法),那麼咱們能夠經過BindingAdapter 註解來實現一個自定義的setter;好比咱們但願給ImageView設置了一個字符串URL的值的時候,ImageView可以根據這個URL進行自主的聯網下載圖片的操做。ide
此外咱們還能夠覆蓋原有的setter方法的邏輯,好比咱們使用 BindingAdapter 的時候參數傳入的是 android:text ,那麼咱們方法的實現邏輯就會複寫原來的setText(String)的方法邏輯了佈局
@Target(ElementType.METHOD)
public @interface BindingAdapter {
/** * @return The attributes associated with this binding adapter. */
String[] value();
/** * Whether every attribute must be assigned a binding expression or if some * can be absent. When this is false, the BindingAdapter will be called * when at least one associated attribute has a binding expression. The attributes * for which there was no binding expression (even a normal XML value) will * cause the associated parameter receive the Java default value. Care must be * taken to ensure that a default value is not confused with a valid XML value. * * @return whether or not every attribute must be assigned a binding expression. The default * value is true. */
boolean requireAll() default true;
}複製代碼
@BindingAdapter("android:bufferType")
public static void setBufferType(TextView view, TextView.BufferType bufferType) {
view.setText(view.getText(), bufferType);
}複製代碼
在上面的例子中,只要咱們的TextView中設置了 android:bufferType 這個屬性,那麼 setBufferType 這個方法就會被回調學習
@BindingAdapter("android:onLayoutChange")
public static void setOnLayoutChangeListener(View view, View.OnLayoutChangeListener oldValue,View.OnLayoutChangeListener newValue) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (oldValue != null) {
view.removeOnLayoutChangeListener(oldValue);
}
if (newValue != null) {
view.addOnLayoutChangeListener(newValue);
}
}
}複製代碼
@BindingAdapter({"android:onClick", "android:clickable"})
public static void setOnClick(View view, View.OnClickListener clickListener, boolean clickable) {
view.setOnClickListener(clickListener);
view.setClickable(clickable);
}複製代碼
當咱們同時設置了多個屬性的時候,要注意參數的順序問題,方法當中的屬性值參數順序必須跟註解當中的屬性參數順序一致
官方代碼註釋中使用示例比較多,並且也很全面,這裏主要展現一下使用 BindingAdapter 註解的實例方法的使用。
首先是咱們的XML佈局文件:
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<data >
<variable name="privateUser" type="net.uni_unity.databindingdemo.model.PrivateUser" />
</data>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<TextView app:text="@{privateUser}" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" />
</LinearLayout>
</layout>複製代碼
接下來是咱們要建立一個對象,裏面包含了咱們的 BindingAdapter 註解的方法:
public class MyBindindAdapter {
@BindingAdapter("app:text")
public void setText(View textView, PrivateUser user) {
Log.d("setText", "isCalled");
}
}複製代碼
而後是咱們的activity:
public class MainActivity extends AppCompatActivity {
private PrivateUser user;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//經過DataBindingUtil加載咱們的佈局文件
//在這裏咱們實例化了一個咱們定義的DataBindingComponent對象MyComponent
//這個是很是關鍵的一個地方,不然系統會使用DataBindingUtil#getDefaultComponent()拿到的默認實例做爲參數
//若是咱們在此以前沒有調用DataBindingUtil.setDefaultComponent()方法,上面的方法就會返回null
ActivityMainBinding activityBinding = DataBindingUtil.setContentView(this, R.layout.activity_main,new MyComponent());
//將view與數據進行綁定
user = new PrivateUser("privateFistName", "privateLastName", "private_user", 10);
activityBinding.setPrivateUser(user);
}複製代碼
接下來就是咱們實現了 DataBindingComponent 的對象
public class MyComponent implements android.databinding.DataBindingComponent {
@Override
public MyBindindAdapter getMyBindindAdapter() {
return new MyBindindAdapter();
}
}複製代碼
咱們會發現 getMyBindindAdapter() 這個方法是帶有 @Override 註解的,這是由於咱們使用 BindingAdapter 註解一個實例方法的時候,data-binding library都會爲咱們在 DataBindingComponent 的實現中自動生成一個getter方法。
通過上面幾步咱們就完成了使用 BindingAdapter 註解實例方法。其中的 關鍵的環節有兩個:
由於官方的代碼註釋中給出了很多的實例,這裏主要說一下咱們使用 BindingAdapter 註解的時候要注意的問題
BindingAdapter 是如何起做用的呢?其實 關鍵的地方主要是兩點 :
BindingAdapter 註解定義的參數,好比@BindingAdapter("android:onLayoutChange")
BindingAdapter 當中的參數的命名空間能夠隨便定義,咱們可使用「android」,固然也可使用「app」,只要參數的值跟佈局文件中定義的值是同樣的就能夠起做用;
方法當中的定義的參數,好比setOnLayoutChangeListener(View view, View.OnLayoutChangeListener oldValue,View.OnLayoutChangeListener newValue)
方法當中第一個參數對應的就是咱們綁定的view對象,能夠是具體的咱們綁定的view,好比TextView,也能夠是對應的父類,好比說view。第二/三個參數對應的是綁定的屬性對應的value值,並且這個value值的類型要跟屬性裏面使用的value值是一致的,不然會不起做用,甚至是報錯。例如:
我在佈局文件中寫了下面的一段代碼:
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<data >
<variable name="privateUser" type="net.uni_unity.databindingdemo.model.PrivateUser" />
</data>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<TextView app:text="@{privateUser}" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" />
</LinearLayout>
</layout>複製代碼
而後我在activity當中使用 BindingAdapter 註解了這樣的一個方法:
@BindingAdapter("app:text")
public static void setText(View textView, String user) {
Log.d("setText", "isCalled");
}複製代碼
這個時候咱們編譯的時候就會收到這樣的error:
java.lang.RuntimeException: Found data binding errors.
/ data binding error msg:Cannot find the setter for attribute 'app:text' with parameter type net.uni_unity.databindingdemo.model.PrivateUser on android.widget.TextView. file:/DataBindingDemo/app/src/main/res/layout/activity_main.xml loc:29:24 - 29:34 \ data binding error
由於咱們在方法中定義的參數類型是String ,但是咱們在XML文件中定義的類型是 PrivateUser ,所以就提示咱們找不到參數匹配的setter方法了。
若是咱們把方法中參數隨便去掉一個,好比咱們定義以下:
@BindingAdapter("app:text")
public static void setText(View textView) {
Log.d("setText", "isCalled");
}複製代碼
這時候咱們就會獲得另外一個編譯錯誤:
java.lang.RuntimeException: failure, see logs for details.@BindingAdapter setSimpleText(java.lang.String) has 1 attributes and 0 value parameters. There should be 1 or 2 value parameters.
上面的兩個錯誤示例大概可以讓咱們明白 BindingAdapter 是怎樣工做的了。
當咱們須要給view綁定的數據類型和view對應屬性的目標類型不一致的時候,咱們一般的作法是先把數據類型轉換以後再與view進行綁定。可是咱們使用了 BindingConversion 註解以後,就能夠定義一個轉換器實如今調用setter方法設置屬性值的時候對數據進行轉換。
/** * Annotate methods that are used to automatically convert from the expression type to the value * used in the setter. The converter should take one parameter, the expression type, and the * return value should be the target value type used in the setter. Converters are used * whenever they can be applied and are not specific to any attribute. */
@Target({ElementType.METHOD})
public @interface BindingConversion {
}複製代碼
根據官方代碼中的註釋,咱們能夠理解到 BindingConversion 的用途:使用該註解的方法能夠自動的將咱們聲明的參數類型轉化成咱們實際要使用的參數類型的值。註解的方法接受一個參數,這個參數的類型就是咱們綁定的view對象聲明的類型,而方法的返回值則是咱們綁定的屬性值的目標類型。要注意的一點是,這種轉換器能夠被使用在任什麼時候候,而並不須要對應特定的屬性。
首先,咱們在佈局文件中定義以下:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data >
<variable name="date" type="java.util.Date"/>
</data>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@{date}" android:padding="5dp" />
</LinearLayout>
</layout>複製代碼
原本 TextView 的 android:text 屬性是接受一個 String 類型的值的,可是咱們在XML當中綁定的倒是一個 Date 類型的值。若是這時候咱們沒有在java文件中定義Converter的話,那麼咱們將會在編譯期獲得這樣的一個error:
java.lang.RuntimeException: Found data binding errors.
/ data binding error msg:Cannot find the setter for attribute 'android:text' with parameter type java.util.Date on android.widget.TextView. file:/DataBindingDemo/app/src/main/res/layout/activity_main.xml loc:35:28 - 35:31 \ data binding error
可是在咱們定義了下面的以後,一切就妥妥的了:
@BindingConversion
public static String convertDate(Date date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}複製代碼
java.lang.RuntimeException: failure, see logs for details.
@BindingConversion is only allowed on public static methods convertDate(java.util.Date)
有的屬性咱們的view屬性的setter方法跟屬性的名稱並不相匹配(由於data-bing是經過setAttr的形式去尋找對應的setter方法的)。好比說「android:tint」這個屬性對應的setter方法名稱是 setImageTintList(ColorStateList) ,而不是 setTint() 方法。這時候使用 BindingMethod 註解能夠幫助咱們從新命名view屬性對應的setter方法名稱。
/** * Used within an {@link BindingMethods} annotation to describe a renaming of an attribute to * the setter used to set that attribute. By default, an attribute attr will be associated with * setter setAttr. */
@Target(ElementType.ANNOTATION_TYPE)
public @interface BindingMethod {
/** * @return the View Class that the attribute is associated with. */
Class type();
/** * @return The attribute to rename. Use android: namespace for all android attributes or * no namespace for application attributes. */
String attribute();
/** * @return The method to call to set the attribute value. */
String method();
}複製代碼
根據官方代碼中的註釋咱們能夠發現,BindingMethod 是放在 BindingMethods 註解當中做爲參數數組的一個元素進行使用的。
使用的時候,咱們須要在註解參數中指定三個關鍵值,分別是:
@BindingMethods({
@BindingMethod(type = "android.widget.ImageView",
attribute = "android:tint",
method = "setImageTintList"),
})複製代碼
BindingMethods 與 BindingMethod 結合使用,使用場景和示例能夠參考上面的說明
/** * Used to enumerate attribute-to-setter renaming. By default, an attribute is associated with * setAttribute setter. If there is a simple rename, enumerate them in an array of * {@link BindingMethod} annotations in the value. */
@Target({ElementType.TYPE})
public @interface BindingMethods {
BindingMethod[] value();
}複製代碼
以上就是關於android data-binding library中的定義的註解的重點學習,主要是結合註解的定義進行簡單的實踐,包括使用的示例,使用場景以及本身的踩坑心得。
固然相對於這些註解,其實更加核心的部分是data-binding library 針對這些註解的所定義的註解處理器的核心實現,關於這部分的解析,我會在後面的文章中繼續分析。
參考文獻:
感謝你寶貴的時間閱讀這篇文章,歡迎你們關注我,也歡迎你們評論一塊兒學習,個人我的主頁淺唱android。