我想用绑定创建一个电影海报图像的网格视图.
我的viewmodel看起来像这样:
public class PopularMoviesViewModel extends BaseObservable {
Movie movie;
Context context;
MovieServiceComponent movieServiceComponent = DaggerMovieServiceComponent.builder()
.contextModule(new ContextModule(context))
.build();
Picasso getPicasso = movieServiceComponent.getPicasso();
public PopularMoviesViewModel(Movie movie, Context context) {
this.movie = movie;
this.context = context;
}
@Bindable
public String getImageUrl(){
return movie.posterPath();
}
@Bindable
public String getTitle(){
return movie.originalTitle();
}
@BindingAdapter({"imageUrl"})
public void setImageUrl(ImageView view, String poserPath){
getPicasso.with(view.getContext()).load("http://image.tmdb.org/t/p/w185"+ poserPath).into(view);
}
}
Run Code Online (Sandbox Code Playgroud)
布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<data class="PopularMoviesBinding">
<variable
name="pmvm"
type="com.hartyandi.oviesm.modelviews.PopularMoviesViewModel"></variable>
</data>
<LinearLayout
android:id="@+id/row"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:paddingBottom="0dp" …Run Code Online (Sandbox Code Playgroud) Android DataBinding库是我学习MVVM的迷人库。现在存在一个问题,即如何在基于布局的基础上将文本更新到UI之前播放动画。(不是BindingAdapter使用静态绑定适配器的全局布局的解决方案。)
从IO16视频中,我知道也许可以DataBindingComponent像setImageUrl示例一样使用它来实现这种效果,但是我找不到关于DataBindingComponents和BindingAdapter注释实例方法的工作方式的示例代码,有人可以提供有关此细节的信息吗?
== 更新2016-07-06 ==
我知道我可以使用带有自定义标签的静态绑定适配器,但这不是我想要的。
== 更新2017-08-04 ==我不知道为什么这个问题被标记为重复,如果您知道android数据绑定,则另一个问题完全不同。只是不知道如何删除重复标记,因此请在此处进行编辑。