2016-08-10 73 views
0

我正在使用回收站查看使用asysnc任務顯示圖像。我正在從Web服務器獲取圖像。我的代碼是Android:如何移動回收站查看圖像請求

從onBindViewHolder梅索德

我打電話

new ImageLoadTask(url, holder.imageView).execute(); 

我ImageLoader的asysnc任務是

public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> { 

     private String url; 
     private ImageView imageView; 
     ProgressDialog pDialog; 

     public ImageLoadTask(String url, ImageView imageView) { 
      this.url = url; 
      this.imageView = imageView; 
     } 

     @Override 
     protected Bitmap doInBackground(Void... params) { 
      try { 
       URL urlConnection = new URL(url); 
       HttpURLConnection connection = (HttpURLConnection) urlConnection 
         .openConnection(); 
       connection.setDoInput(true); 
       connection.connect(); 

       InputStream input = connection.getInputStream(); 
       Bitmap myBitmap = BitmapFactory.decodeStream(input); 
       return myBitmap; 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Bitmap result) { 
      super.onPostExecute(result); 
      imageView.setImageBitmap(result); 
     } 
    } 

問題是,當我滾動並跳過一個或多個視圖被下載的圖像之前和視圖被重新使用,圖像donwnload reqest不會在這些中間視圖上被取消,導致在該視圖中加載實際圖像之前該視圖/這些圖像會一閃而過。

我試着從適配器中傳遞HttpURLConnection,並檢查它是否爲空,然後在onBindViewHolder方法上調用disconnect,但仍然發生。我正在使用

if (holder.urlConnection != null) 
    { 
     holder.urlConnection.disconnect(); 
     try { 
      holder.urlConnection.getInputStream().close(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
     holder.urlConnection = null; 
    } 

    new ImageLoadTask(url, holder.imageView,holder.viewHolderActivity, holder.urlConnection).execute(); 

如何取消圖像請求?

ImageLoadTask imageLoadTask=new ImageLoadTask(url, holder.imageView); 
imageLoadTask.cancel(true); 

回答

0

嘗試使用cancel()方法,以消除圖像請求取消的AsyncTask。 onBindViewHolder方法中的 通過調用其cancel方法取消現有ImageLoadTask併爲新圖像創建新任務。請注意,當AsyncTask被取消時,它將不會調用onPostExecute方法,而會調用onCancelled

1

在您ViewHolder保持對它的引用您的ImageLoadTask

0
在支架

保存ImageLoadTask鏈接

if (holder.urlConnection != null) 
     { 
      holder.urlConnection.disconnect(); 
      try { 
       holder.urlConnection.getInputStream().close(); 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } 
      holder.urlConnection = null; 
     } 

     holder.imageTask = new ImageLoadTask(url, holder.imageView,holder.viewHolderActivity, holder.urlConnection); 
     holder.imageTask.execute(); 

,並取消其在

//Called when a view created by this adapter has been recycled. 
public void onViewRecycled(VH holder){ 
     holder.imageTask.cancel(); 
} 
0

使用滑行: https://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

這是加載圖像的快速反應框架,從任何給定的背結束。你可以做像setTimeout,cancelRequests,fadeEffects和其他東西的東西。您甚至不必擔心如何處理網絡請求。滑翔只會爲你做。

用法:

Glide.with(context) 
    .load(imageLoadPath) 
    .placeholder(imgToShowWhileLoading) 
    .centerCrop() 
    .into(imageView); 

然後就可以設置GlideModule配置是這樣的: (原始代碼片段:glide image loading timeout increase)。製作一個自定義類來實現GlideModule。在其中的一個壓倒一切的方法:

@Override 
public void registerComponents(Context context, Glide glide) { 
    final int retryPolicy = 10000; 
    RequestQueue queue = new RequestQueue(
      new DiskBasedCache(new File(context.getCacheDir(), "volley")), 
      new BasicNetwork(new HurlStack())) { 
     @Override public <T> Request<T> add(Request<T> request) { 
      request.setRetryPolicy(new DefaultRetryPolicy(retryPolicy, 1, 1); 
      return super.add(request); 
     } 
    }; 
    queue.start(); 
    glide.register(GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(queue)); 
} 

玩超時限制所有你想要的,看看它是如何能夠滿足您的需求。