2014-04-02 32 views
0

我想在listitem按鈕上顯示一個動畫。動畫效果很好。我沒有點擊的其他按鈕也顯示動畫。我發現問題將出現在適配器的回收視圖中。誰能幫我處理這種情況。這裏是我寫的代碼:適配器getview方法中:listview的適配器不工作perefct

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); 
    } 
    }); 

回答

0

您應該以某種方式在getView(...)刪除動畫,所以每一個觀點被重複使用時它會重置動畫。 我建議以下思路:

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 */); 
} 

唯一的問題是,動畫甚至會消失,它應該存在的項目。 對於這種情況,您可以通過存儲一組動畫項目的位置來修改解決方案。

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); 
} 

當然,當您刪除動畫(網絡執行後)時,您應該從集合中刪除位置。