对于GridView中的第一个位置,多次调用getView

时间:2012-10-10 16:46:13

标签: android

我有一个带有此getView的适配器:

public View getView(int position, View convertView, ViewGroup parent) {
    Log.d("getView gv", position+"");

    NewsLine holder = null;

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.grid_entry, parent, false);
        holder = new NewsLine();

        holder.iv= (ImageView) convertView.findViewById(R.id.photo);

        convertView.setTag(holder);
    } else {
        holder = (NewsLine) convertView.getTag();
    }

    NewsItem item=news.ITEMS.get(position);
    //---------

    if(item.imgurl!=null && item.imgurl.compareToIgnoreCase("null")!=0) 
    {
        holder.iv.setVisibility(View.VISIBLE);     
        mMemoryCache.loadBitmap(item.imgurl, holder.iv,position);
    }
    else
        holder.iv.setVisibility(View.INVISIBLE);
    //-------------




    return convertView;
}

我有两个问题:

  1. 对于位置0多次调用getView(如果在LruCache中丢失了位图,则使用AsyncTask下载位图)。我有一个动画(从0到1的alpha),为该位置重新启动几次。

  2. 因为我正在回收视图,有时您可以在几分之一秒内看到旧的imageView内容。

  3. // ----

    这是缓存类(只有堆):

    public class SetImgAT extends LruCache<String, Bitmap> {
    private static SetImgAT instance;   
    private Animation FadeInAnimation;
    
    private SetImgAT(int size, Context context) {
        super(size);
        FadeInAnimation = AnimationUtils.loadAnimation(context, R.anim.fadein);
    }
    
    public static synchronized SetImgAT getInstance(int size, Context context) {
        if (instance == null) {
            instance = new SetImgAT(size, context);
        }
        return instance;
    }
    
    @Override
    protected int sizeOf(String key, Bitmap value) {
        return (value.getRowBytes() * value.getHeight());
    }
    
    public void loadBitmap(String url, ImageView imageView,int pos) {
        Bitmap bitmap = instance.get(url.hashCode() + "");
        if (bitmap != null) {
            Log.d("ImageCache", "hit - "+url.hashCode()+"pos:"+pos);
            imageView.setImageBitmap(bitmap);
    
            imageView.invalidate();
        } else {
            Log.d("ImageCache", "miss");
            BitmapWorkerTask task = new BitmapWorkerTask(imageView);
            task.execute(url);
        }
    }
    
    class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
        ImageView mImageView;
    
        public BitmapWorkerTask(ImageView imageView) {
            mImageView = imageView;
        }
    
        @Override
        protected Bitmap doInBackground(String... url) {
            Bitmap Picture = null;
    
    
            if (url[0] != null && url[0].compareToIgnoreCase("null") != 0) {
                Log.d("GetBMP from", url[0]);
    
                URL img_value = null;
                try {
                    img_value = new URL(url[0]);
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
                try {
                    Picture = BitmapFactory.decodeStream(img_value
                            .openConnection().getInputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (Picture == null) {
                    Log.d("deb", "no bitmap");
                } else {
                    Log.d("got deb", "got bitmap to "+url[0].hashCode());                   
                    instance.put(url[0].hashCode()+"", Picture);
                }
    
            }
            return Picture;
        }
    
        @Override
        protected void onPostExecute(Bitmap result) {
            // super.onPostExecute(result);
            if (result != null) {
                Log.d("deb", "set bitmap");
                mImageView.setImageBitmap(result);
                //mImageView.startAnimation(FadeInAnimation);
            }
    
        }
    }
    

    // ----------------

    }

    谢谢! :)

1 个答案:

答案 0 :(得分:0)

我在来回滚动或随意调用notifyDataSetChanged()时看到了类似的行为。

作为对你现在正在做的事情的改进,我建议使用Picasso,因为除了动画淡入之外它还能很好地处理这个案例。

getView()中的一个衬垫:

Picasso.with(context).load(urlToLoad).into(imageView);

请参阅: http://square.github.io/picasso/