如何在 ListView
中为新添加的项目添加动画效果?
我有一个 adapter
,当我向列表中添加新项目时,我会调用adapter.notifyDataSetChanged();
,项目被添加了,一切都运行得很完美,但我的问题是我希望新添加的元素有一些动画效果。
Animate
每个添加到您的Custom Adapter
的元素,在getView()
方法中。
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;
}
从而实现 Animation
。
View
进行动画处理:这是一种相当复杂的方法,但我找到的唯一一种向ListView
的子项添加动画的方法如下:
您可以尝试通知适配器您希望删除的项目的ID,并调用adapter.notifyDataSetChanged();
。这将生成对适配器的getView()
方法的调用。在其中,您可以执行以下操作:
if ( item.getId() == itemToRemove ) {
//apply the animation
}
adapter.notifyDataSetChanged()
来将所有内容放置在正确的位置。
LinearLayout
而不是ListView
。 - inazaruk