我设置了我的 imageview layout width to a fix value
,my layout height to wrapcontent
. imageview 的图像是通过 url 加载的。发生的事情是,当我的视图被绘制时,由于图像尚未加载,它只会绘制layout_width
. 但是当图像已经加载时,不再绘制高度,因此不会显示图像。只有当我来回滚动时才会显示它,因为我猜那是视图将被重绘的时间。有没有办法解决这个问题?使用invalidate()
无效。也许我做错了?
我将发布我的代码片段:这是扩展 BaseAdaptergetView
的MyAdapter:
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
Typeface typeFaceMyriadReg = Typeface.createFromAsset(context.getAssets(), MYRIAD_REG);
if(convertView == null){
convertView = inflater.inflate(R.layout.shop_list_layout, null);
holder = new ViewHolder();
holder.txtShopName = (TextView) convertView.findViewById(R.id.name);
holder.txtShopName.setTypeface(typeFaceMyriadReg);
holder.txtShopDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtShopDesc.setTypeface(typeFaceMyriadReg);
holder.imageView = (ImageView) convertView.findViewById(R.id.prof_pic);
holder.progBar = (ProgressBar) convertView.findViewById(R.id.img_progress);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
imageLoader=new ImageLoader(context.getApplicationContext(), holder.progBar);
ShopRowItem shopItem = (ShopRowItem) getItem(position);
holder.txtShopName.setText(shopItem.getShopName());
holder.txtShopDesc.setText(shopItem.getShopDesc());
imageLoader.DisplayImage(shopItem.getImageUrl(), holder.imageView);
return convertView;
}
然后是 ImageLoder 的一部分:
public void DisplayImage(String url, ImageView imageView)
{
imageViews.put(imageView, url);
Bitmap bitmap=memoryCache.get(url);
if(bitmap!=null){
imageView.setImageBitmap(bitmap);
progressBar.setVisibility(View.VISIBLE);
}
else
{
queuePhoto(url, imageView);
progressBar.setVisibility(View.VISIBLE);
}
}
// ...
// ...
//Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable
{
Bitmap bitmap;
PhotoToLoad photoToLoad;
public BitmapDisplayer(Bitmap b, PhotoToLoad p){bitmap=b;photoToLoad=p;}
public void run()
{
if(imageViewReused(photoToLoad))
return;
if(bitmap!=null){
//should i do recalculations and adjustments here? ? ?
photoToLoad.imageView.setImageBitmap(bitmap);
progressBar.setVisibility(View.GONE);
}
else{
photoToLoad.imageView.setImageResource(stub_id);
progressBar.setVisibility(View.VISIBLE);
}
}
}