【问题标题】:Gmail like listview item removeGmail like listview 项目删除
【发布时间】:2013-01-13 15:42:03
【问题描述】:

我正在尝试实现 Gmail 应用 (ICS) 在删除邮件时提供的功能。我不想删除单元格下方的所有行都向上移动并覆盖已删除的单元格。

这是工作动画:

<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 中为它们设置动画。我担心这是性能灾难。我也很难通知适配器它应该重新加载它的内容。通常我会在onAnimationEnd 上致电notifyDataSetChanged,但现在有几个动画一个接一个地播放。

朋友们有什么建议吗?也许有什么东西可以刺激地为几个视图设置动画?

【问题讨论】:

  • 最简单的方法是减少删除行的高度。

标签: android android-listview android-animation


【解决方案1】:

更新::我建议查看在 Android 团队工作的 Chet Haase 的 this solution。特别是如果您不是为 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);
}

【讨论】:

  • @matthias 你能告诉我哪个视图是你在 removeRow 方法中传递的视图吗?它是列表视图还是我从我的布局 xml 膨胀的根视图!
  • 是列表项视图。
【解决方案2】:

本文的原作者在此处将代码作为 Gist 发布:https://gist.github.com/2980593 这是来自 Roman Nurik 的原始 Google+ 帖子:https://plus.google.com/113735310430199015092/posts/Fgo1p5uWZLu

【讨论】:

  • 不幸的是它适用于 API 12+
【解决方案3】:

你可以试试我为此制作的 ListView。它在Github

【讨论】:

    猜你喜欢
    • 2012-06-22
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    相关资源
    最近更新 更多