Android数据绑定添加小部件作为绑定适配器的参数

时间:2016-08-24 11:08:07

标签: android data-binding android-recyclerview android-databinding

我想运行可见的进度条,直到图像加载到imageview中。我想通过绑定适配器实现它。我有一个CustomBinder类,它具有imageUrl的绑定节点。这是我的自定义活页夹 -

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中,他们是一个进度条,我想看到progrssbar,直到图像加载完成。那是我的带数据绑定的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 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);
            }
        });

现在在上面的代码中,我第一次看到进度条,然后当图像加载完成时,我只是进入了进度条。现在同样的approch我将如何使用@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();
    }


}

1 个答案:

答案 0 :(得分:4)

使用BindingAdapter方法

绑定另一个参数
@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