2013-01-13 35 views
1

我正在嘗試實現Gmail應用(ICS)在刪除郵件時提供的功能。我不想刪除下面的所有行,刪除單元格向上移動並覆蓋已刪除的單元格。Gmail like listview item remove

這裏正在動畫:

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false" > 

     <translate android:fromYDelta="0%" android:toYDelta="-100%" 
      android:duration="@android:integer/config_mediumAnimTime"/> 
     <alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
      android:duration="@android:integer/config_mediumAnimTime" /> 

</set> 

所有我想出了到目前爲止是這樣的:

public static List<View> getCellsBelow(ListView listView, int position) { 
    List<View> cells = new ArrayList<View>();  

    for (int i = position + 1; i <= listView.getLastVisiblePosition(); i++) { 
     cells.add(listView.getChildAt(i)); 
    } 

    return cells; 
} 

我收集可見單元格婁選定單元格,然後在動畫的foreach他們。我擔心這是性能災難。我也有問題通知適配器,它應該重新加載它的內容。通常我會打撥打電話notifyDataSetChanged,但現在有幾個動畫連續播放。

任何建議好朋友?也許有什麼東西可以激發幾個觀點的興奮點?

+0

最簡單的方法是減少刪除行的高度。 – yDelouis

+2

要不要考慮那個拍攝自己的球...... –

回答

6

更新::我建議由切特·哈澤檢查出this solution誰在Android團隊工作。特別是如果你不開發Android 2.3及更低版本。


這應該就是你想要的。

list.setOnItemLongClickListener(new OnItemLongClickListener() { 

    @Override 
    public boolean onItemLongClick(AdapterView<?> parent, 
      final View view, final int position, long id) { 
     removeRow(view, position); 
     return true; 
    } 
}); 

private void removeRow(final View row, final int position) { 
    final int initialHeight = row.getHeight(); 
    Animation animation = new Animation() { 
     @Override 
     protected void applyTransformation(float interpolatedTime, 
       Transformation t) { 
      super.applyTransformation(interpolatedTime, t); 
      int newHeight = (int) (initialHeight * (1 - interpolatedTime)); 
      if (newHeight > 0) { 
       row.getLayoutParams().height = newHeight; 
       row.requestLayout(); 
      } 
     } 
    }; 
    animation.setAnimationListener(new AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 
     } 
     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 
     @Override 
     public void onAnimationEnd(Animation animation) { 
      row.getLayoutParams().height = initialHeight; 
      row.requestLayout(); 
      items.remove(position); 
      ((BaseAdapter) list.getAdapter()).notifyDataSetChanged(); 
     } 
    }); 
    animation.setDuration(300); 
    row.startAnimation(animation); 
} 
+0

好..幫助我..謝謝 – Hima

+0

@matthias ..爲我工作..謝謝! –

+0

@matthias你能告訴我哪個視圖是你在removeRow方法中傳遞的視圖嗎?是列表視圖還是我從我的佈局xml膨脹的rootview? – k2ibegin

1

您可以嘗試我爲此製作的ListView。它在Github

+0

謝謝,偉大的代碼! –