Animate newly added items in ListView
How can I animate newly added items in ListView
?
I have a adapter
and when I add new items in my list I say adapter.notify开发者_高级运维DataSetChanged();
the items are added, everything works perfectly, but my problem is I want newly added element to have some animation.
Animate
each added element in the getView()
method of your 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;
}
And thereby achieve the Animation
.
The official docs about animation in Android say that you can set an animation to trigger whenever the layout is changed, using android:animateLayoutChanges="true".
Taken from: http://developer.android.com/training/animation/layout.html
Adding this kind of animations is harder than I first thought of. There are two ways depending of the kind of animation you are trying to achieve.
Using LayoutAnimationController. There is an example in the API demos.
Animating each
View
:
This is quite a hack but the only way I found to add an animation to ListView
's children is the following:
You can try notifying the adapter the id of the item you are willing to delete and call adapter.notifyDataSetChanged();
. This will generate calls to the adapter's getView()
method. Inside it you can do something like:
if ( item.getId() == itemToRemove ) {
//apply the animation
}
After the animation finished you can recall adapter.notifyDataSetChanged()
to put everything in place.
精彩评论