Android数据绑定添加小部件作为绑定适配器的参数
作者:互联网
我想运行可见的进度条,直到图像加载到imageview中.我想通过绑定适配器实现它.我有一个具有imageUrl的bindingadpter的CustomBinder类.这是我的自定义活页夹
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 onl oadingStarted(String imageUri, View view) {
}
@Override
public void onl oadingFailed(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 onl oadingComplete(String imageUri, View view, Bitmap loadedImage) {
}
});
}
}
在我的xml中,它们是一个进度条,我想看到该进度条,直到图像加载完成.那是我的数据绑定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>
这是在我只是简单地使用绑定而不是通过调用BindingAdapter调用视图之前.
viewDataBinding.progressBar.setVisibility(View.VISIBLE);
imageLoader.displayImage(artListModel.getImage(), viewDataBinding.artImg, options, new SimpleImageLoadingListener() {
@Override
public void onl oadingStarted(String imageUri, View view) {
}
@Override
public void onl oadingFailed(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 onl oadingComplete(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方法绑定
@BindingAdapter({"imageUrl","progressbar"})
public static void loadImage(ImageView view, String imageUrl, ProgressBar progressBar){
...
...
@Override
public void onl oadingComplete(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-recyclerview,data-binding,android-databinding,android 来源: https://codeday.me/bug/20191026/1938881.html