【问题标题】:Animate certain ListView items为某些 ListView 项目设置动画
【发布时间】:2012-03-14 21:36:57
【问题描述】:

我的目标是通过在自定义ArrayAdapter 中用新膨胀的列表项替换列表项来为某些ListView 项设置动画,而不必担心getView 会弄乱动画。

如果我使用convertView 来避免膨胀新项目,动画的顺序会随机变化。

手动缓存视图可以正常工作,但我怀疑这是一个好的解决方案。更好的想法?

【问题讨论】:

    标签: android listview animation android-arrayadapter


    【解决方案1】:

    我所做的是在 convertview 上设置动画,然后在每个 convertview 上停止动画。这样,如果转换视图是新的,动画就会停止然后播放,如果在结束前没有被回收,则继续播放直到结束。

    编辑我似乎找不到示例,所以部分是伪代码。

    在您的适配器中,您将拥有如下内容:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
    
        if(convertView == null) {
            // setup holder
            holder = new ViewHolder();
    
            LayoutInflater Inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = Inflater.inflate(mResourceId, null);
    
            holder.image = (ImageView) convertView.findViewById(R.id.row_image);
            holder.headline = (TextView) convertView.findViewById(R.id.row_headline);
    
            convertView.setTag(holder);
        } else {
            // get existing row view
            holder = (ViewHolder) convertView.getTag();
        }
        // GetView is only called on new items 
        // so now we stop the previous animation and start a new
        holder.animation.stop();  // First you stop the animation
        holder.animation = new animation();  // then you create new
        holder.animation.start();  // then you start the animation.
    

    【讨论】:

    • @AshrafSayied-Ahmad 我添加了一个小例子,它并不完美,但你可以开玩笑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2014-12-15
    • 2020-05-19
    • 1970-01-01
    相关资源
    最近更新 更多