滚动时,我的GridView的某些图像混淆了。我不明白为什么......
这是我的代码:
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Item curItem = items.get(position);
ViewHolder holder;
if (convertView == null){ // if it's not recycled, initialize some attributes
convertView = mInflater.inflate(R.layout.grid_cell, null);
holder = new ViewHolder();
holder.squareImageView = (SquareImageView)convertView.findViewById(R.id.item_image);
convertView.setTag(holder);
holder.itemNameTextView = (TextView)convertView.findViewById(R.id.itemNameTextView);
holder.deleteImageView = (SquareImageView)convertView.findViewById(R.id.deleteImageView);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
if(!curItem.loadingLaunched )
{
curItem.loadingLaunched = true;
curItem.loadImage();
}
if(curItem.bm != null)
{
holder.squareImageView.setImageBitmap(curItem.bm);
}
if(curItem.title != null)
{
holder.itemNameTextView.setText(curItem.title);
}
return convertView;
}
static class ViewHolder {
SquareImageView squareImageView;
TextView itemNameTextView;
SquareImageView deleteImageView ;
}
有人看到如何解决这个问题吗?
谢谢!
答案 0 :(得分:0)
好的,发现了问题!
在我的Item类中,我添加了类似的loadImage方法:
bm=null; // Bad idea to init this BitMap to null !!!!!!
try {
UrlImage = new URL (file);
HttpURLConnection connection;
connection = (HttpURLConnection) UrlImage.openConnection();
bm= BitmapFactory.decodeStream(connection.getInputStream());
}
catch{...}
这是一个非常糟糕的主意,因为只要未加载远程图像,curItem.bm就保持为null。并且gridView不喜欢加载空图像(它混合了所有内容)。
所以,在item类的构造函数中(警告:将它放在构造函数中非常重要,奇怪的是如果只使用而不是" bm = null"),我把它放在:
bm = BitmapFactory.decodeResource(MyApp.appContext.getResources(), R.drawable.default_image);
不再混合了!