2013-01-13 36 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

谢谢,伟大的代码! –