Thahzan
Thahzan

Reputation: 1005

Android - Setting animations to individual items in ListView

I tried the following code to implement a slide bottom in (Google+ like) animation to my ListView items.

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

    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row_layout, null);
    }

    convertView.setVisibility(View.INVISIBLE);

    //Setting values to list items.

    convertView.startAnimation(AnimationUtils.loadAnimation(context, R.anim.abc_slide_in_bottom));
    convertView.setVisibility(View.VISIBLE);

    return convertView;
}

The animation is working, the list items are pushed from bottom. But the problem is, the already available items at the top repeat the animation when I'm scrolling down and it looks really weird. And when I scroll up, the animation is repeated again. I'm pretty sure this is not the correct way to implement this animation. Is there a way to implement this without using any libraries?

EDIT : I extended from BaseAdapter to implement the adapter.

Upvotes: 1

Views: 833

Answers (1)

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

If you want just the animation to work only when you scroll down you need to detect if you scroll down the listview and start the animation.

sample:

create a global instance of lastPosition in your BaseAdapter

int lastPosition;

After use that lastPosition to detect the if you are scrolling down or up

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

    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.row_layout, null);
    }

    //if scroll down then play animation
    if(position <= lastPosition)
       convertView.startAnimation(AnimationUtils.loadAnimation(context, R.anim.abc_slide_in_bottom));

    convertView.setVisibility(View.INVISIBLE);    

    convertView.setVisibility(View.VISIBLE);

    lastPosition = position;

    return convertView;
}

Upvotes: 2

Related Questions