如何在recyclelerview项目中加载图像之前添加微调器

时间:2016-10-15 14:59:46

标签: android android-recyclerview android-spinner

我正在使用数据绑定来获取布局和Glide库从Adapter类加载图像。现在我想在每行加载图像之前附加进度微调器,但是目前,我不知道如何加载?谢谢

这是我的Adapter类

public class ImageSetAdapter extends RecyclerView.Adapter<ImageSetAdapter.PictureItemHolder> {

    List<HouseModelSet> data;
    HousesListener housesListener;

    public ImageSetAdapter(List<HouseModelSet> data) {
        this.data = data;
    }

    @Override
    public PictureItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
         /*View v = inflater.inflate(R.layout.item_home_picture, parent, false);
            ItemHomePictureBinding itemHomePictureBinding = DataBindingUtil.inflate(inflater, R.layout.item_home_picture, parent, false);*/

        ItemHomePictureBinding binding = ItemHomePictureBinding.inflate(inflater, parent, false);

        return new PictureItemHolder(binding.getRoot());
    }

       @Override
    public void onBindViewHolder(PictureItemHolder holder, final int position) {
       HouseModelSet houseSetModel = data.get(position);
       List<PostModel> postModels = houseSetModel.getHouseModels();
       if (postModels.size() > 4) {
           final PostModel hm1 = postModels.get(0);
           final PostModel hm2 = postModels.get(1);
           final PostModel hm3 = postModels.get(2);
           final PostModel hm4 = postModels.get(3);
           final PostModel hm5 = postModels.get(4);

           holder.binding.house1.setHouse(hm1);
           holder.binding.house2.setHouse(hm2);
           holder.binding.house3.setHouse(hm3);
           holder.binding.house4.setHouse(hm4);
           holder.binding.house5.setHouse(hm5);
           holder.binding.setHouses(houseSetModel);

           holder.binding.house1.getRoot().setOnClickListener(new View.OnClickListener(){
               @Override
               public void onClick(View v) {
                   if (housesListener != null) {
                       housesListener.onHouseClick(position, hm1);
                   }
               }
           });

           holder.binding.house2.getRoot().setOnClickListener(new View.OnClickListener(){
               @Override
               public void onClick(View v) {
                   if (housesListener != null) {
                       housesListener.onHouseClick(position, hm2);
                   }
               }
           });

           holder.binding.house3.getRoot().setOnClickListener(new View.OnClickListener(){
               @Override
               public void onClick(View v) {
                   if (housesListener != null) {
                       housesListener.onHouseClick(position, hm3);
                   }
               }
           });

           holder.binding.house4.getRoot().setOnClickListener(new View.OnClickListener(){
               @Override
               public void onClick(View v) {
                   if (housesListener != null) {
                       housesListener.onHouseClick(position, hm4);
                   }
               }
           });

           holder.binding.house5.getRoot().setOnClickListener(new View.OnClickListener(){
               @Override
               public void onClick(View v) {
                   if (housesListener != null) {
                       housesListener.onHouseClick(position, hm5);
                   }
               }
           });

       }
    }

    @BindingAdapter("app:imageUrl")
    public static void loadImage(ImageView imageView, String url) {
        Glide.with(imageView.getContext())
                .load(ApiHelper.BASE_URL + url)
                .thumbnail(0.5f)
                .crossFade()

                .into(imageView);
    }

    public void setHousesListener(HousesListener housesListener) {
        this.housesListener = housesListener;
    }

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

    public class PictureItemHolder extends RecyclerView.ViewHolder{

        ItemHomePictureBinding binding;

        public PictureItemHolder(View itemView) {
            super(itemView);
            binding = DataBindingUtil.bind(itemView);
        }

    }

    public void updateList(List<HouseModelSet> list) {
        this.data = list;
        notifyDataSetChanged();
    }

    public interface HousesListener{
        void onHouseClick(int pos, PostModel houseModel);
    }
}

1 个答案:

答案 0 :(得分:2)

也许你可以添加监听器来显示微调器的进度。

您可以将代码更改为:

Glide.with(imageView.getContext())
        .load(ApiHelper.BASE_URL + url)
.listener(new RequestListener<String, GlideDrawable>() {
    @Override
    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
        yourSpinnerProgress.setVisibility(View.GONE);
        return false;
    }

    @Override
    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
        yourSpinnerProgress.setVisibility(View.GONE);
        return false;
    }
})
.into(imageView);

并且不要忘记将RelativeLayout用于布局的根目录&amp;将yourSpinnerProgress添加到您的布局中。