用适配器在网格视图中动画最后添加的孩子



我有一个GridView,我不断添加视图。当视图添加到网格中时,我希望它做一个动画。然而,由于我必须使用setAdapter()来刷新GridView,它最终会使所有视图动画化,因为它们都被重新添加。还有别的办法吗?

下面是我要添加的视图的代码:

public class GridImageView extends ImageView {

public GridImageView(Context context) {
    super(context);
}
public GridImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
}
public GridImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}
@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    ScaleAnimation anim = new ScaleAnimation(0,1,0,1);
    anim.setDuration(1000);
    anim.setFillAfter(true);
    this.startAnimation(anim);
 }
}

一如既往,感谢您的帮助

多亏了Luksprog的建议,我已经在我的自定义视图中设置了一个标志,它将决定视图在添加到网格视图时是否应该动画。

public class GridImageView extends ImageView
{
   private boolean _animate = false;
   public GridImageView(Context context) {
       super(context);
   }
   public GridImageView(Context context, AttributeSet attrs) {
       super(context, attrs);
   }
   public GridImageView(Context context, AttributeSet attrs, int defStyle) {
       super(context, attrs, defStyle);
   }
   @Override
   protected void onAttachedToWindow() {
       if(_animate){
            super.onAttachedToWindow();
            ScaleAnimation anim = new ScaleAnimation(0,1,0,1);
            anim.setDuration(1000);
            anim.setFillAfter(true);
            this.startAnimation(anim);
       }
   }

   public void set_animate(boolean _animate) {
       this._animate = _animate;
   }

}

和我的适配器在其GetView()函数中检查它是否是数组列表中的最后一个,如果是,则将标志设置为true。

    if( i == ( _gridDetailsArrayList.size() - 1 ))
        holder.gridImage.set_animate(true);

最新更新