通用图像加载器设置不相关的图像

时间:2017-12-07 14:25:17

标签: android android-recyclerview universal-image-loader

我正在使用Universal Image Loader库,以便在我的Recycler视图中设置来自服务器的图像。 在我的回收器视图中,当我开始滚动它时,它将设置不相关的图像几秒钟,然后,它将设置真实的图像。 我不知道问题出在哪里。

这是我正在使用的适配器:

public class HistoryDetailAdapter extends RecyclerView.Adapter<HistoryDetailAdapter.ViewHolder> {
  private List<PList> menuItems;
  private Context mContext;
  private ActivityHistoryDetails activityHistoryDetails;
  private DisplayImageOptions defaultOptions;
  private ImageLoaderConfiguration config;

  public HistoryDetailAdapter(List<PList> menuItems, Context mContext) {
    this.menuItems = menuItems;
    this.mContext = mContext;
    this.activityHistoryDetails = (ActivityHistoryDetails) mContext;

    byte[] toEncrypt = (G.getPremiumState()[0] + ":" + G.getPremiumState()[1]).getBytes();
    String encryptedCredentials = Base64.encodeToString(toEncrypt, Base64.DEFAULT);
    Map<String, String> headers = new HashMap();
    headers.put("Authorization", "Basic " + encryptedCredentials);

    defaultOptions = new DisplayImageOptions.Builder()
      .cacheInMemory(false)
      .cacheOnDisk(false)
      .resetViewBeforeLoading(true)
      .delayBeforeLoading(5000)
      .showImageOnLoading(R.drawable.loading_02)
      .showImageOnFail(R.drawable.ic_empty_white_box)
      .extraForDownloader(headers)
      .build();

    config = new ImageLoaderConfiguration.Builder(activityHistoryDetails)
      .defaultDisplayImageOptions(defaultOptions)
      .imageDownloader(new AuthDownloader(activityHistoryDetails))
      .build();

  }

  public static class ViewHolder extends RecyclerView.ViewHolder {

    ImageView imgDefault;
    LinearLayout parentLayout;
    CoordinatorLayout coordinatorLayout;

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


      imgDefault = (ImageView) v.findViewById(R.id.img_defaultImage);
      parentLayout = (LinearLayout) v.findViewById(R.id.parentLayout);
      coordinatorLayout = (CoordinatorLayout) v.findViewById(R.id.coordinatorLayout);

    }
  }

  @Override
  public HistoryDetailAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    // Create a new View
    final View v = LayoutInflater.from(activityHistoryDetails).inflate(R.layout.activity_history_detail_recycler, parent, false);
    ViewHolder vh = new ViewHolder(v);
    return vh;
  }

  @Override
  public void onBindViewHolder(final ViewHolder holder, final int position) {

    //Download and Load Default Image from server into imgDefault ImageView
    String url = "http://192.168.14.77:8080/api/images/download/";

    if (menuItems.get(position).getPDefaultImage().getIId() != null) {

      url += menuItems.get(position).getPDefaultImage().getIId();

      ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance
      imageLoader.init(config);
      imageLoader.handleSlowNetwork(true);

      // Load image, decode it to Bitmap and return Bitmap to callback
      ImageSize targetSize = new ImageSize(120, 120); // result Bitmap will be fit to this size
      imageLoader.loadImage(url, targetSize, new SimpleImageLoadingListener() {
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
          // Do whatever you want with Bitmap
          holder.imgDefault.setImageBitmap(loadedImage);
        }
      });

    } else {
       holder.imgDefault.setImageResource(R.drawable.loading_01);
    }

  }

  @Override
  public int getItemCount() {
    if (menuItems.size() > 0) {

      return menuItems.size();

    } else {
      return 0;
    }

  }

  public List<PList> getList() {
    return menuItems;
  }
}

感谢您的回答。

1 个答案:

答案 0 :(得分:1)

您可以使用Glide或Fresco图像库。他们工作稳定。