Squeazer
Squeazer

Reputation: 1254

Animating list items with animations

I have an Activity with multiple pages each one containing a ListView. I'm using an adapter for custom list items. Each item is animated into the list with this:

public View getView(int i, View view, ViewGroup viewGroup) {
    if (view == null) {
        v = inflater.inflate(R.layout.list_item, null);
        doTheThing = true;
    }

    //Some random code here

    if(doTheThing) {
        Animation animation = AnimationUtils.loadAnimation(a, R.anim.list_item_animation);
        animation.setStartOffset(i * 60 + 200);
        v.startAnimation(animation);
    }

    return v;
}

With this setup i have two problems. The first one is, that when i scroll more than two pages left or right the animation repeats, it should only run the first time the activity is opened. The second problem is that if i scroll through the list quickly there are some items missing ocasinally and then they animate back in. It's always random and when they are all loaded it doesnt happen again.

How can i fix this?

Upvotes: 1

Views: 238

Answers (1)

Jay Bobzin
Jay Bobzin

Reputation: 1020

Basing "doTheThing" off of whether or not the convertView is null is not going to be entirely deterministic.

The Adapter doesn't guarantee if/when it is going to recycle views, and when it is going to provide you new ones.

Of course, you're guaranteed to get new views for the first page worth of views in the list view. However, the Adapter might decide it suddenly needs 4 views instead of 3 to keep up smooth scrolling, in which case you would get view == null, and trigger an animation.

I recommend relying on a more deterministic way of deciding which views to animate. For instance, all views that are retrieved within 500ms of the first view being retrieved will be animated, etc.

Upvotes: 1

Related Questions