本文介绍使用Android Data Binding技术,结合豆瓣电影搜索API,在RecycleView中展示电影列表。
代码实现
我根据 MVVM(Model-View-ViewModel) 的顺序介绍代码实现。
Model层
Model层我定义了一个 Movie 类,要获得通知UI更新的功能,需要继承 BaseObservable 类;
如果要能被View绑定,需要在get方法上添加 @Bindable 标注,同时在set方法中要调用 notifyPropertyChanged(BR.title) 方法通知View更新。
BR类会根据 @Bindable 标注自动生成
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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);
}
...
}
|
我定义了一个getDescription方法,将original_title和year组合在一起,
在View层也能进行绑定,getDescription相关的字段值发生变化后,
需要在set方法中调用 notifyPropertyChanged 通知View更新内容。
View层
需要在布局文件中使用 layout 作为最外层布局,同时在其中的 data 区域中声明一个movie变量,
并指定类型为我们在Model中定义的 Movie 类。
在View中使用@{movie.title}这样的表达式, 将变量进行绑定。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
<
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之后,需要把两者连起来,当Model的数据变化后,自动更新View。
Android Data Binding中的ViewModel是根据layout自动生成的Binding类,
如果layout的名称是 movie_item.xml ,生成的Binding类名称就是MovieItemBinding。
-
创建Binding类在RecyclerView的Adapter的onCreateViewHolder中创建Binding类
12MovieItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.movie_item, parent,
false
);
-
设置变量值在onBindViewHolder中设置layout中定义的movie变量值
123Movie movie = mMovies.get(position);
holder.binding.setVariable(com.aswifter.databinding.BR.movie, movie);
holder.binding.executePendingBindings();
-
显示图片使用Glide显示图片,ivMovie是MovieItemBinding根据ImageView的id自动生成的
1234Glide.with(MovieActitiviy.
this
)
.load(movie.getImages().getMedium())
.fitCenter()
.into(holder.binding.ivMovie);
DataBinding参考
可以阅读以下文章:
一些感想
- Model的定义还是比较麻烦,需要写标注和通知方法,希望后续有相应的Android Studio插件自动生成。
- Binding类自动生成View对应的字段,不用写findViewById了,这点很爽。
- 代码结构更清晰,使用MVVM模式分层的思想,Model层只负责数据,
我把网络请求和Json解析的方法都写到Model中,Activity基本上与Model解耦了,
以后更换接口,HTTP库或JSON库,都不需要动Activity,代码测试也更方便。 - Data Binding目前只能实现单向绑定,期待以后能支持双向绑定。
Github
代码已经发布到Github,