【问题标题】:Display multiple images at the same time using picasso into gridview in android在android中使用毕加索在gridview中同时显示多张图像
【发布时间】:2018-01-15 15:21:09
【问题描述】:

请帮帮我。我试过这段代码,但它在不同时间显示图像。我需要同时显示所有图像。谢谢你的帮助

Picasso.with(getContext()).load(result.thumListUrl).fetch();
                Picasso
                        .with(getContext())
                        .load(result.thumListUrl)
                        .networkPolicy(NetworkPolicy.OFFLINE)
                        .fit()
                        .into(img, new Callback() {
                            @Override
                            public void onSuccess() {
                                Log.d("Picasso", "Image loaded from cache>>>" + result.thumListUrl);
                            }

                            @Override
                            public void onError() {
                                Log.d("Picasso", "Try again in ONLINE mode if load from cache is failed");
                                Picasso.with(getContext()).load(result.thumListUrl).fit().into(img, new Callback() {
                                    @Override
                                    public void onSuccess() {
                                        Log.d("Picasso", "Image loaded from web>>>" + result.thumListUrl);
                                    }

                                    @Override
                                    public void onError() {
                                        Log.d("Picasso", "Failed to load image online and offline, make sure you enabled INTERNET permission for your app and the url is correct>>>>>>>" + result.thumListUrl);
                                    }
                                });
                            }
                        });

【问题讨论】:

    标签: android gridview picasso


    【解决方案1】:

    URL 加载images 并让它在不同时间获取数据时加载,而不是隐藏imageview 或在其顶部用占位符imageview 覆盖它,直到所有images已加载。

    在片段中

     private int loadedImages = 0;
        private int numberOfImagesToBeLoaded = 2;
        ArrayList<ImageView> images = new ArrayList<>();
        addImageUsingPicasso(url1,img1);
        addImageUsingPicasso(url2,img2);
    
      public void addImageUsingPicasso(String url,ImageView img){
         images.add(img);
        private Target target = new Target() {
              @Override
              public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                   img.setImageBitmap(bitmap);
                   loadedImages++;
                   if(loadedImages == numberOfImagesToBeLoaded) { //<--- All images are loaded
                      showImages(); 
                   }
              }
    
              @Override
              public void onBitmapFailed(Drawable errorDrawable) {
                  loadedImages++;
              }
    
              @Override
              public void onPrepareLoad(Drawable placeHolderDrawable) {
              }
           }
         Picasso.with(getContext()).load(url).into(target);
       }
    
    
      private void showImages(){
         for(ImageView image : images){
             image.setVisibility(View.VISIBLE);
          }
       }
    

    【讨论】:

    • 我这里只有片段。我必须创建适配器吗?或者我可以在我的片段中使用你的代码吗?
    猜你喜欢
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    相关资源
    最近更新 更多