Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate certain ListView items

My goal is to animate certain ListView items without having to worry about getView messing with the animations by replacing list items with newly inflated ones in the custom ArrayAdapter.

If I use convertView to avoid inflating new items the order of the animations changes randomly.

Caching the views manually works fine but I doubt that it is a good solution. Better ideas?

like image 729
John Avatar asked Oct 09 '22 09:10

John


1 Answers

What I do is to set animation at convertview and then I stop animation on each convertview. This way the animation is stopped and then played if the convertview is new and continue until end if it isn't recycled before it ends.

Edit I can't seem to find an example so it will be partly pseudo code.

In your adapter you'll have something like following:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;

    if(convertView == null) {
        // setup holder
        holder = new ViewHolder();

        LayoutInflater Inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = Inflater.inflate(mResourceId, null);

        holder.image = (ImageView) convertView.findViewById(R.id.row_image);
        holder.headline = (TextView) convertView.findViewById(R.id.row_headline);

        convertView.setTag(holder);
    } else {
        // get existing row view
        holder = (ViewHolder) convertView.getTag();
    }
    // GetView is only called on new items 
    // so now we stop the previous animation and start a new
    holder.animation.stop();  // First you stop the animation
    holder.animation = new animation();  // then you create new
    holder.animation.start();  // then you start the animation.
like image 180
Warpzit Avatar answered Oct 12 '22 10:10

Warpzit