动画化ArrayAdapter的延迟加载

时间:2013-05-09 22:26:11

标签: android animation lazy-loading android-arrayadapter

您好,我正在为ArrayAdapter使用动画。我想在加载时为缩略图设置动画...但是每次加载列表的任何缩略图时,都会为数组适配器的每个项启动动画。结果,每个缩略图的动画开始5次。在加载任何项目时,我该怎么做才能阻止开始动画?

public View getView(int position, View item, ViewGroup parent){

        ViewHolder holder;
        Video video = mVideoList.get(position);

        if(item == null)
        {
            item = mInflater.inflate(R.layout.adapter_recommended_videos, null);

            holder = new ViewHolder();
            holder.title = (TextView)item.findViewById(R.id.adapter_recommended_videos_textview);
            holder.thumb = (ImageView)item.findViewById(R.id.adapter_recommended_videos_imageview);

            holder.title.setTypeface(
                    Typeface.createFromAsset(mContext.getAssets(), "roboto_medium.ttf"));

            item.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) item.getTag();

        }

        holder.title.setText(video.getTitle());
        ImageView iv = holder.thumb;
        if (video.getThumb() != null) {
            if(!mAnimationFlags.get(position)){
                iv.startAnimation(mAnimation);
                mAnimationFlags.set(position, true);
            }
            holder.thumb.setImageBitmap(video.getThumb());
        } else {
            holder.thumb.setImageResource(R.drawable.dummy_video_thumbnail);
        }


        return(item);
    }


    static class ViewHolder {
        TextView title;
        ImageView thumb;
    }

1 个答案:

答案 0 :(得分:1)

没关系,我自己解决了,我只需要在每次调用'getView()'时实例化一个新动画。这是正确的代码:

public View getView(int position, View item, ViewGroup parent){

        ViewHolder holder;
        Video video = mVideoList.get(position);

        if(item == null)
        {
            item = mInflater.inflate(R.layout.adapter_recommended_videos, null);

            holder = new ViewHolder();
            holder.title = (TextView)item.findViewById(R.id.adapter_recommended_videos_textview);
            holder.thumb = (ImageView)item.findViewById(R.id.adapter_recommended_videos_imageview);

            holder.title.setTypeface(
                    Typeface.createFromAsset(mContext.getAssets(), "roboto_medium.ttf"));

            item.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) item.getTag();

        }

        holder.title.setText(video.getTitle());
        ImageView iv = holder.thumb;
        if (video.getThumb() != null) {
            if(!mAnimationFlags.get(position)){
                Animation anim = AnimationUtils.loadAnimation(mContext, android.R.anim.fade_in);
                iv.startAnimation(anim);
                mAnimationFlags.set(position, true);
            }
            holder.thumb.setImageBitmap(video.getThumb());
        } else {
            holder.thumb.setImageResource(R.drawable.dummy_video_thumbnail);
        }


        return(item);
    }