Shu*_*dia 5 android static-methods android-databinding
所以我使用这个流行的数据绑定代码片段通过传入URL将图像加载到列表项的imageview中:
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp""
app:imageUrl="@{movie.imageUrl}"
/>
Run Code Online (Sandbox Code Playgroud)
绑定适配器:
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.
});
}
Run Code Online (Sandbox Code Playgroud)
如果我只是使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.
Run Code Online (Sandbox Code Playgroud)
通过自定义投标适配器放置整个电影实例:
\n\n <ImageView\n android:layout_width="match_parent"\n android:layout_height="150dp""\n app:movie="@{movie}"\n />\nRun Code Online (Sandbox Code Playgroud)\n\n绑定适配器:
\n\nclass Movie{\nboolean isLoaded;\n\n@BindingAdapter({"movie"})\n public static void loadImage(final ImageView view, final Movie movie) {\n if(movie != null &&\xc2\xa0moview.getImageUrl() != null){\n Picasso.with(view.getContext())\n .load(movie.getImageUrl())\n .into(view, new Callback.EmptyCallback() {\n @Override public void onSuccess() {\n image.setLoaded(true);\n });\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2289 次 |
| 最近记录: |