2017-05-07 67 views
0

如何正確創建動畫(在列表視圖中)?ArrayListAdapter - ListView動畫

我有這樣的代碼:

public void blink(final View v) 
{ 
    final Animation animation = new AlphaAnimation(1, 0); 
    animation.setDuration(500); 
    animation.setInterpolator(new LinearInterpolator()); 
    animation.setRepeatCount(Animation.INFINITE); 
    animation.setRepeatMode(Animation.REVERSE); 

    v.startAnimation(animation); 

    new Thread(new Runnable() 
    { 
     @Override 
     public void run() 
     { 
      try { Thread.sleep(2000); } 
      catch (InterruptedException e) { e.printStackTrace(); } 

      v.clearAnimation(); 
     } 
    }).start(); 
} 

,並查看在 「getView」 功能(ArrayListAdapter)動畫:

if(!alarm.isAnimated()) 
{ 
    blink(view); 
    alarm.setAnimated(true); 
} 

這工作不錯,但我需要經常更新視圖每一個更新取消我的動畫...

notifyDataSetChanged(); // calls getView, isAnimated = true, animation is canceled 

而且我不想只重置動畫(它可能會結束在中間,它不會看起來不錯),我也只想當我添加項目到列表視圖(不更新時)的動畫...

感謝您的幫助。

編輯:

整個適配器:

public class AlertListAdapter extends ArrayAdapter<AlarmEntity> 
{ 
    private ArrayList<AlarmEntity> data; 

    public AlertListAdapter(Context context, ArrayList<AlarmEntity> data) 
    { 
     super(context, android.R.layout.simple_list_item_2, data); 
     this.data = (data); 
    } 

    @NonNull 
    @Override 
    public View getView(int position, View convertView, @NonNull ViewGroup parent) 
    { 
     LayoutInflater inflater = LayoutInflater.from(getContext()); 
     @SuppressLint("ViewHolder") View view = inflater.inflate(R.layout.alert_row, parent, false); 

     return getView(getItem(position), view); 
    } 

    private View getView(AlarmEntity alarm, View view) 
    { 
     if(!alarm.isAnimated()) 
     { 
      blink(view); 
      alarm.setAnimated(true); 
     } 

     return (view); 
    } 

    public void update() 
    { 
     for(AlarmEntity e : data) 
      e.setDistance(Coords.getDistance(CarEntity.location, e.getLocation())); 

     notifyDataSetChanged(); 
    } 

    public void blink(final View v) 
    { 
     final Animation animation = new AlphaAnimation(1, 0); 
     animation.setDuration(500); 
     animation.setInterpolator(new LinearInterpolator()); 
     animation.setRepeatCount(Animation.INFINITE); 
     animation.setRepeatMode(Animation.REVERSE); 

     v.startAnimation(animation); 

     new Thread(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       try { Thread.sleep(2000); } 
       catch (InterruptedException e) { e.printStackTrace(); } 

       v.clearAnimation(); 
      } 
     }).start(); 
    } 
} 
+0

發表您的適配器 –

+0

的代碼實現,如果它可以幫助你(但我寫的一切)更好 - 我打電話只是適配器.update(); –

回答

0

簡單的方法是不使用長時間的動畫添加到細胞的ListView。 :-D

public void pushIn(final View v) 
{ 
    Animation animation = AnimationUtils.loadAnimation(MainActivity.getContext(), R.anim.push_left_anim); 
    animation.setDuration(500); 
    v.startAnimation(animation); 
} 

它看起來比眨眼:-D

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <translate android:fromXDelta="100%p" android:toXDelta="0" 
     android:duration="300"/> 
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
     android:duration="300" /> 
</set>