【发布时间】:2015-11-24 21:49:26
【问题描述】:
所以我使用这个流行的数据绑定代码 sn-p 通过传入 URL 将图像加载到列表项的图像视图中:
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp""
app:imageUrl="@{movie.imageUrl}"
/>
绑定适配器:
class Movie{
boolean isLoaded;
@BindingAdapter({"bind:imageUrl"})
public static void loadImage(final ImageView view, String imageUrl) {
Picasso.with(view.getContext())
.load(imageUrl)
.into(view, new Callback.EmptyCallback() {
@Override public void onSuccess() {
//set isLoaded to true for the listview item
// but cannot access boolean isLoaded as it is non static.
});
}
如果我只是将BindingAdapter 设为非静态,则会引发错误:
java.lang.IllegalStateException: Required DataBindingComponent is null in class MovieTileBinding. A BindingAdapter in com.example.moviesapp.Pojos.Results is not static and requires an object to use, retrieved from the DataBindingComponent. If you don't use an inflation method taking a DataBindingComponent, use DataBindingUtil.setDefaultComponent or make all BindingAdapter methods static.
【问题讨论】:
-
这篇很棒的博文将向您展示如何使用非静态 BindingAdapter realm.io/news/data-binding-android-boyar-mount。
-
查看这个重复问题的解决方案:stackoverflow.com/a/38216344/1650674
标签: android static-methods android-databinding