2014-11-03 64 views
0

我有一個girdview顯示圖像和文本,當滾動gridview時,圖像的順序發生變化,文本保持正確,但圖像發生變化。我用Viewholder模式米,但仍然有同樣的問題..下面是我的代碼:GridView項目在滾動後發生變化

@Override 
public View getView(int position, View v, ViewGroup parent) { 
    // Keeps reference to avoid future findViewById() 
    final ViewHolder viewHolder; 
    if (v == null) { 
     LayoutInflater li = (LayoutInflater) getContext().getSystemService(
       Context.LAYOUT_INFLATER_SERVICE); 
     v = li.inflate(R.layout.row_recipe, parent, false); 

     viewHolder = new ViewHolder(); 
     viewHolder.tvRecipeName = (TextView) v.findViewById(R.id.txtUserName); 
     viewHolder.ivRecipe = (ImageView) v.findViewById(R.id.ivRecipe); 
     viewHolder.tvLikes = (TextView) v.findViewById(R.id.tvLikes); 
     viewHolder.loader = (ProgressBar) v.findViewById(R.id.loader); 

     v.setTag(viewHolder); 
    } else { 
     viewHolder = (ViewHolder) v.getTag(); 
    } 

    final Recipe recipe = values.get(position); 
    if (recipe != null) { 
     viewHolder.tvRecipeName.setText(recipe.name); 
     viewHolder.tvLikes.setText(String.valueOf(recipe.likesCount)); 


     // Thread to load image of the recipe 
     Photo mainPhoto = recipe.getMainPhoto(); 
     if (mainPhoto != null && mainPhoto.name != null 
       && !mainPhoto.name.equals("null")) { 
      mainPhoto.format = CompressFormat.JPEG; 
      mainPhoto.quality = 80; 
      mainPhoto.sample = true; 

      // Calculate inSampleSize 
      Display display = mActivity.getWindowManager() 
        .getDefaultDisplay(); 
      Point size = new Point(); 
      display.getSize(size); 
      int width = size.x; 
      int height = size.y; 
      int reqWidth = width/2; 
      double widthHeightRatio = (int) Math.ceil(height/width); 
      int reqHeight = (int) Math.ceil(reqWidth * widthHeightRatio); 

      mainPhoto.width = reqWidth; 
      mainPhoto.height = reqHeight; 

      // setup a listener to just change the one image view instead of 
      // calling notifyDataSetChanged which redraws the entire list 
      // view screen. 

      mainPhoto.listener = new ImageCacheListener() { 
       @Override 
       public void onSuccess(Bitmap bitmap, Photo photo) { 
        super.onSuccess(bitmap, photo); 
        viewHolder.ivRecipe.setImageBitmap(bitmap); 
        viewHolder.loader.setVisibility(View.INVISIBLE); 
       } 

       @Override 
       public void onError(Throwable throwable, Photo photo) { 
        super.onError(throwable, photo); 
       } 
      }; 

      // get the friend image or the default 
      Bitmap recipePhoto = remoteImageCache.getImage(mainPhoto); 

      if (recipePhoto != null) 
       viewHolder.ivRecipe.setImageBitmap(recipePhoto); 
     } else { 
      viewHolder.loader.setVisibility(View.INVISIBLE); 
     } 
    } 
    return v; 
} 

static class ViewHolder { 
    TextView tvLikes; 
    ProgressBar loader; 
    ImageView ivRecipe; 
    TextView tvRecipeName; 
} 

回答

相關問題