gpt4 book ai didi

Android Data Binding 添加widget作为绑定(bind)适配器的参数

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:47 25 4
gpt4 key购买 nike

我想运行可见的进度条,直到图像加载到 ImageView 中。我想通过绑定(bind)适配器来实现它。我有一个 CustomBinder 类,它有一个 imageUrl 的绑定(bind)适配器。这是我的自定义 Binder -

public class CustomBinders {

static ImageLoader imageLoader;
public static DisplayImageOptions options;
private static final String TAG = "CustomBinders";

@BindingAdapter({"imageUrl"})
public static void loadImage(ImageView view, String imageUrl){
imageLoader= ImageLoader.getInstance();
options = StaticMethods.setUIL();
Log.d(TAG, "loadImage: ");
imageLoader.displayImage(imageUrl, view, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {

}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
String message = null;
switch (failReason.getType()) {
case IO_ERROR:
message = "Input/Output error";
break;
case DECODING_ERROR:
message = "Image can't be decoded";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}
view.setBackgroundResource(R.drawable.blank);

}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

}
});
}
}

在我的 xml 中,它们是一个进度条,我想在图像加载完成之前显示进度条。那是我的带有数据绑定(bind)的 xml。

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<data class="ArtListCreatedByBind">
<variable
name="artlist"
type="com.twebexponent.artwork.data.ArtListCreatedBy"/>
</data>

<FrameLayout
android:id="@+id/myContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:background="@drawable/manageartwork_border">

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:visibility="gone" />

<ImageView
android:id="@+id/art_img"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
app:imageUrl="@{artlist.image}"
/>
</FrameLayout>



</layout>

这是以前的情况,当时我只是使用绑定(bind)而不是调用 BindingAdapter 来调用 View 。

viewDataBinding.progressBar.setVisibility(View.VISIBLE);
imageLoader.displayImage(artListModel.getImage(), viewDataBinding.artImg, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {

}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
String message = null;
switch (failReason.getType()) {
case IO_ERROR:
message = "Input/Output error";
break;
case DECODING_ERROR:
message = "Image can't be decoded";
break;
case NETWORK_DENIED:
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:
message = "Out Of Memory error";
break;
case UNKNOWN:
message = "Unknown error";
break;
}

viewDataBinding.progressBar.setVisibility(View.GONE);
viewDataBinding.artImg.setBackgroundResource(R.drawable.blank);

}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
viewDataBinding.progressBar.setVisibility(View.GONE);
}
});

现在在上面的代码中,我第一次看到进度条,然后当图像加载完成时,我只是离开了进度条。现在同样的方法我将如何使用@BindingAdapter({"imageUrl"}) 来做到这一点??

这是我的 recylerview 适配器的样子-

public class ArtistCreatedByAdapter extends RecyclerView.Adapter<ArtistCreatedByAdapter.CustomViewHolder> {


ArrayList<ArtListCreatedBy> createdByArtList;
Activity activity;
LayoutInflater inflater;
ImageLoader imageLoader;
public DisplayImageOptions options;
ArtListCreatedByBind viewDataBinding;

public class CustomViewHolder extends RecyclerView.ViewHolder {

private ArtListCreatedByBind mViewDataBinding;

public CustomViewHolder( View v) {
super(v);

mViewDataBinding = DataBindingUtil.bind(v);
mViewDataBinding.executePendingBindings();

}

public ArtListCreatedByBind getViewDataBinding() {
return mViewDataBinding;
}
}

public ArtistCreatedByAdapter(Activity activity, ArrayList<ArtListCreatedBy> createdByArtList) {
this.activity = activity;
imageLoader= ImageLoader.getInstance();
this.createdByArtList = createdByArtList;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.singlerow_art_list_created_by, parent, false);

return new CustomViewHolder(itemView);
}

@Override
public void onBindViewHolder(CustomViewHolder holder, final int position) {
ArtListCreatedBy artListModel= createdByArtList.get(position);
viewDataBinding = holder.getViewDataBinding();

viewDataBinding.setArtlist(artListModel);


}

@Override
public int getItemCount() {
return createdByArtList.size();
}


}

最佳答案

使用您的BindingAdapter 方法绑定(bind)另一个参数

@BindingAdapter({"imageUrl","progressbar"})
public static void loadImage(ImageView view, String imageUrl, ProgressBar progressBar){
...
...
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(View.GONE);
}
}

现在像这样从 xml 传递你的 progressBar :

<ImageView
android:id="@+id/art_img"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
app:imageUrl="@{artlist.image}"
app:progressbar="@{progressBar}"/>

注意:这里的 progressBar 将是您的 ProgressBar

的 id

关于Android Data Binding 添加widget作为绑定(bind)适配器的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39121649/

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