在ListView中为新添加的项目添加动画

Luk*_*kap 13 animation android listview adapter

如何为新添加的项目添加动画ListView

我有一个adapter,当我在列表中添加新项目时,我说adapter.notifyDataSetChanged();项目已添加,一切正常,但我的问题是我想要新添加的元素有一些动画.

ASH*_*ASH 24

AnimategetView()你方法中每个添加的元素Custom Adapter.

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

    View v = convertView;

    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.simple_list_item_1, null);
    }

    ListData o = list.get(position);
    TextView tt = (TextView) v.findViewById(R.id.toptext);

    tt.setText(o.content);

    Log.d("ListTest", "Position : "+position);
    if(flag == false) {
        Animation animation = AnimationUtils
                .loadAnimation(getActivity(), R.anim.slide_top_to_bottom);
        v.startAnimation(animation);
    }
    return v;
}
Run Code Online (Sandbox Code Playgroud)

从而实现了Animation.

  • @ASH"if(flag == false)"行中的那个标志变量是什么? (4认同)

小智 7

Android中关于动画的官方文档说,你可以使用android:animateLayoutChanges ="true"设置动画,以便在布局发生变化时触发.

取自:http://developer.android.com/training/animation/layout.html


Mac*_*rse 5

添加这种动画比我想象的更难.根据您尝试实现的动画类型,有两种方式.

这是一个非常黑客但我发现为ListView孩子们添加动画的唯一方法如下:

您可以尝试通知适配器您愿意删除和调用的项目的ID adapter.notifyDataSetChanged();.这将生成对适配器getView()方法的调用.在它里面你可以做类似的事情:

if ( item.getId() == itemToRemove ) {
 //apply the animation
}
Run Code Online (Sandbox Code Playgroud)

动画结束后,你可以回想起adapter.notifyDataSetChanged()把所有东西放到位.