Subho Subho - 3 months ago 33
Android Question

Android Data Binding Add widget as a parameter of binding adapter

I want to run the visible the progressbar until the image load into imageview. I want to implement it through binding adapter. I have a CustomBinder class which has a bindingadpter of imageUrl. This is my custom 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) {

}
});
}
}


In my xml their is a progressbar and i want to visible the progrssbar untill the image loading is complete. That is my xml with data bind.

<?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>


And this was before when i simply calling the view using binding not by calling BindingAdapter.

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);
}
});


Now in above code i am visible the progressbar first time and then when the images load has done that time i simply gone the progressbar. Now the same approch how i will do this using @BindingAdapter({"imageUrl"})??

This is what my adapter look like of 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();
}


}

RRR RRR
Answer

Bind another parameter with your BindingAdapter method

@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);
    }
}

now pass your progressBar from xml like this :

<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}"/>