gpt4 book ai didi

android - 使用 Android 数据绑定(bind)时如何访问 BindingAdapter 中的实例变量?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:12:30 25 4
gpt4 key购买 nike

所以我使用这个流行的数据绑定(bind)代码 fragment 通过传入 URL 将图像加载到列表项的 ImageView 中:

  <ImageView
android:layout_width="match_parent"
android:layout_height="150dp""
app:imageUrl="@{movie.imageUrl}"
/>

绑定(bind)适配器:

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.

最佳答案

通过自定义绑定(bind)适配器放置整个电影实例:

 <ImageView
android:layout_width="match_parent"
android:layout_height="150dp""
app:movie="@{movie}"
/>

绑定(bind)适配器:

class Movie{
boolean isLoaded;

@BindingAdapter({"movie"})
public static void loadImage(final ImageView view, final Movie movie) {
if(movie != null && moview.getImageUrl() != null){
Picasso.with(view.getContext())
.load(movie.getImageUrl())
.into(view, new Callback.EmptyCallback() {
@Override public void onSuccess() {
image.setLoaded(true);
});
}
}

关于android - 使用 Android 数据绑定(bind)时如何访问 BindingAdapter 中的实例变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33904519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com