2016-04-26 149 views
0

我綁定到惰性加載圖像到我的ListView,圖像加載正常,但我有一個問題。加載圖像時會互換。Android懶惰的圖像加載ListView無法正常工作

假設ListView有10行。它加載第1行的圖像,將其顯示在第1行,然後加載第2行的圖像。它在第二行顯示片刻,然後顯示第一行第二行的圖像。然後,row1中的ImageView在第1行和第2行的圖像之間切換。同樣,加載下一行的圖像。前一行的圖像會在兩者之間切換。然後加載所有的圖像後,一切都得到正確顯示。

這裏是我的代碼

Adapater類:

public class FamilyMemberListAdapter extends ArrayAdapter<Map<String, String>> { 
    List<Map<String, String>> familyMemberList = new ArrayList<Map<String, String>>(); 
    private Activity activity; 

    public FamilyMemberListAdapter(Activity activity, 
      List<Map<String, String>> familyMemberList) { 
     super(activity, R.layout.activity_gch_family_members, familyMemberList); 
     this.activity = activity; 
     this.familyMemberList = familyMemberList; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 
      convertView = LayoutInflater.from(this.getContext()).inflate(
        R.layout.activity_gch_family_member_item, parent, false); 
      holder = new ViewHolder(); 
      holder.lblFamilyMemberName = (TextView) convertView 
        .findViewById(R.id.lblFamilyMemberItem); 
      holder.lblFamilyMemberRelation = (TextView) convertView 
        .findViewById(R.id.lblFamilyMemberRelationItem); 
      holder.imgProfilePic = (ImageView) convertView 
        .findViewById(R.id.imgvProfilePic); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     int accountId = Integer.valueOf(familyMemberList.get(position).get(
       "accountId")); 
     holder.lblFamilyMemberName.setText("Name: " 
       + familyMemberList.get(position).get("name")); 
     holder.lblFamilyMemberRelation.setText("Relation: " 
       + familyMemberList.get(position).get("relation")); 
     if (holder.imgProfilePic != null) { 
      new ImageDownloaderTask(holder.imgProfilePic).execute(String 
        .valueOf(accountId)); 
     } 
     return convertView; 
    } 

    @Override 
    public int getCount() { 
     return familyMemberList.size(); 
    } 

    static class ViewHolder { 
     TextView lblFamilyMemberName; 
     TextView lblFamilyMemberRelation; 
     ImageView imgProfilePic; 
    } 
} 

ImageLoader的的AsyncTask:

public class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> { 
    private final WeakReference<ImageView> imageViewReference; 

    public ImageDownloaderTask(ImageView imageView) { 
     imageViewReference = new WeakReference<ImageView>(imageView); 
    } 

    @Override 
    protected Bitmap doInBackground(String... params) { 
     String responseText = null; 
     HttpClient httpClient = ServiceHelper.getHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpGet httpGet = new HttpGet(RestApiPaths.GET_PROFILE_PIC + accountId); 
     try { 
      HttpResponse response = httpClient.execute(httpGet); 
      int statusCode = response.getStatusLine().getStatusCode(); 
      final HttpEntity entity = response.getEntity(); 
      if (entity != null) { 
       InputStream inputStream = null; 
       try { 
        // getting contents from the stream 
        inputStream = entity.getContent(); 

        // decoding stream data back into image Bitmap that android understands 
        final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
        return bitmap; 
       } finally { 
        if (inputStream != null) { 
         inputStream.close(); 
        } 
        entity.consumeContent(); 
       } 
      } 
      Log.d(TAG, responseText); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Bitmap bitmap) { 
     if (isCancelled()) { 
      bitmap = null; 
     } 

     if (imageViewReference != null) { 
      ImageView imageView = imageViewReference.get(); 
      if (imageView != null) { 
       if (bitmap != null) { 
        imageView.setImageBitmap(bitmap); 
       } else { 
        Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.holder_pic_side); 
        imageView.setImageDrawable(placeholder); 
       } 
      } 
     } 
    } 
} 

我創建了一個靜態類爲ListView項目。仍然爲什麼圖像在加載時進行交換。請告訴我我在這裏做錯了什麼。

+1

在調用AsyncTask之前,用'holder.imgProfilePic.setTag(accountId);'嘗試。爲您的任務添加一個額外的參數accountId。然後在onPostExecute中檢查它是否與圖像視圖中的accountId相同。 – greenapps

+0

這解決了我的問題,非常感謝 – HeisenBerg

回答

1

在調用AsyncTask之前添加holder.imgProfilePic.setTag(accountId);。爲您的任務添加一個額外的參數accountId。然後在onPostExecute檢查它是否與圖像視圖中的accountId相同

0

也許嘗試使用像picasso或通用圖像加載器或類似的庫。 他們已經解決了大部分圖像加載問題

+0

這不提供問題的答案。要評論或要求作者澄清,在他們的帖子下留下評論 - 你需要獲得[聲譽](http://stackoverflow.com/faq#reputation),然後才能評論他人的帖子以防止濫用;你爲什麼不嘗試並通過[回答問題]獲得一些答案(http://stackoverflow.com/unanswered)? –

+0

這是我嘗試的第一件事。但是我想加載的圖像需要會話。 – HeisenBerg