本文介紹使用Android Data Binding技術(shù),結(jié)合豆瓣電影搜索API,在RecycleView中展示電影列表。

代碼實(shí)現(xiàn)
我根據(jù)MVVM(Model-View-ViewModel)的順序介紹代碼實(shí)現(xiàn)。
Model層
Model層我定義了一個(gè)Movie類,要獲得通知UI更新的功能,需要繼承BaseObservable類;
如果要能被View綁定,需要在get方法上添加@Bindable標(biāo)注,同時(shí)在set方法中要調(diào)用notifyPropertyChanged(BR.title)方法通知View更新。
BR類會根據(jù)@Bindable標(biāo)注自動生成
public class Movie extends BaseObservable {
private String id;
private String title;
private String original_title;
private String year;
private Images images;
private Rating rating;
@Bindable
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
notifyPropertyChanged(BR.title);
}
@Bindable
public String getDescription() {
return this.original_title + "\n" + this.getYear();
}
public void setOriginal_title(String original_title) {
this.original_title = original_title;
notifyPropertyChanged(BR.description);
}
public void setYear(String year) {
this.year = year;
notifyPropertyChanged(BR.description);
}
...
}
我定義了一個(gè)getDescription方法,將original_title和year組合在一起,
在View層也能進(jìn)行綁定,getDescription相關(guān)的字段值發(fā)生變化后,
需要在set方法中調(diào)用notifyPropertyChanged通知View更新內(nèi)容。
View層
需要在布局文件中使用layout作為最外層布局,同時(shí)在其中的data區(qū)域中聲明一個(gè)movie變量,
并指定類型為我們在Model中定義的Movie類。
在View中使用@{movie.title}這樣的表達(dá)式, 將變量進(jìn)行綁定。
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="movie"
type="com.aswifter.databinding.model.Movie" />
</data>
...
<ImageView
android:id="@+id/ivMovie"
android:layout_width="109dp"
android:layout_height="135dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{movie.title}"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="@color/primary_text" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:text="@{movie.description}"
android:textColor="@color/secondary_text" />
<RatingBar
style="@android:style/Widget.DeviceDefault.Light.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:numStars="10"
android:rating="@{movie.rating.average}"
android:stepSize="0.5" />
</LinearLayout>
...
</layout>
ViewModel層
定義好了Model和View之后,需要把兩者連起來,當(dāng)Model的數(shù)據(jù)變化后,自動更新View。
Android Data Binding中的ViewModel是根據(jù)layout自動生成的Binding類,
如果layout的名稱是movie_item.xml,生成的Binding類名稱就是MovieItemBinding。
- 創(chuàng)建Binding類
在RecyclerView的Adapter的onCreateViewHolder中創(chuàng)建Binding類
MovieItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.movie_item, parent, false);
- 設(shè)置變量值
在onBindViewHolder中設(shè)置layout中定義的movie變量值
Movie movie = mMovies.get(position);
holder.binding.setVariable(com.aswifter.databinding.BR.movie, movie);
holder.binding.executePendingBindings();
- 顯示圖片
使用Glide顯示圖片,ivMovie是MovieItemBinding根據(jù)ImageView的id自動生成的
Glide.with(MovieActitiviy.this)
.load(movie.getImages().getMedium())
.fitCenter()
.into(holder.binding.ivMovie);
DataBinding參考
可以閱讀以下文章:
一些感想
- Model的定義還是比較麻煩,需要寫標(biāo)注和通知方法,希望后續(xù)有相應(yīng)的Android Studio插件自動生成。
- Binding類自動生成View對應(yīng)的字段,不用寫findViewById了,這點(diǎn)很爽。
- 代碼結(jié)構(gòu)更清晰,使用MVVM模式分層的思想,Model層只負(fù)責(zé)數(shù)據(jù),
我把網(wǎng)絡(luò)請求和Json解析的方法都寫到Model中,Activity基本上與Model解耦了,
以后更換接口,HTTP庫或JSON庫,都不需要動Activity,代碼測試也更方便。 - Data Binding目前只能實(shí)現(xiàn)單向綁定,期待以后能支持雙向綁定。
Github
代碼已經(jīng)發(fā)布到Github,
源碼地址:AndroidDataBindingExample
本文作者: 陽春面
原文地址:http://www.aswifter.com/2015/07/11/android-data-binding-example/
歡迎關(guān)注我的微信公眾號,分享Android 開發(fā),IOS開發(fā),Swift開發(fā)和互聯(lián)網(wǎng)內(nèi)容
微信號:APP開發(fā)者