0

I want to show an animation on a listitem button. The animation works fine. some other buttons which I didn't clicked also shows the animation.I found that the problem will be with recycling views in adapters. Can anyone help me to handle this situation.. Here is the code I have written: inside getview method of adapter:

viewHolder.getrate.setOnClickListener(
   new CompoundButton.OnClickListener() {
      public void onClick(View paramView) {
         ListData rateobj = (ListData) viewHolder.getrate.getTag();
         paramView.setBackgroundResource(R.drawable.spin_animation);

         // Get the background, which has been compiled to an AnimationDrawable object.
         frameAnimation = (AnimationDrawable) paramView.getBackground();

         // Start the animation (looped playback by default).
         frameAnimation.start();

         NetworkRun nt = new NetworkRun(rateobj);
         String number=rateobj.getDescription();
         String num=number.replaceAll("\\s+","");
         nt.execute(num);

         viewHolder.load.setEnabled(true);
         viewHolder.load.setVisibility(View.VISIBLE);
     }
  });
Luca Sepe
  • 2,435
  • 1
  • 20
  • 26
irfan
  • 869
  • 3
  • 12
  • 25

1 Answers1

0

You should somehow remove animation in getView(...), so every time the view is reused it will reset the animation. I suggest the following idea:

public View getView(int position, View convertView, ViewGroup parent) {
    // ... inflate convertView, create viewHolder, etc.
    convertView.setBackgroundResource(0); // <-- this will remove animation
    viewHolder.getrate.setOnClickListener(/* your code here */);
}

The only problem is that the animation will disappear even for items where it should exist. For this case you may modify the solution by storing the set of animated items' positions.

Set<Integer> animatedPositions = new HashSet<Integer>();
public View getView(final int position, View convertView, ViewGroup parent) {
    // ... inflate convertView, create viewHolder, etc.
    if(animatedPositions.contains(position)) {
         showAnimation(convertView);
    } else {
         hideAnimation(convertView);
    }    
    viewHolder.getrate.setOnClickListener(
      new OnClickListener() {
          public void onClick(View paramView) {
              animatedPositions.add(position);
              showAnimation(paramView);
              // your code
          }
      }
    );
}

private void showAnimation(View view) {
    view.setBackgroundResource(R.drawable.spin_animation);
    AnimationDrawable frameAnimation = (AnimationDrawable) paramView.getBackground();
    frameAnimation.start();
}

private void hideAnimation(View view) {
    convertView.setBackgroundResource(0);
}

Of course, you should remove positions from the set when you remove animations (after network executions).

xapienz
  • 179
  • 5