在iOS中,有一个非常简单和强大的工具来动画添加和删除UITableView行,这是一个来自youtube视频的剪辑,显示默认动画。注意周围的行如何折叠到已删除的行上。这个动画帮助用户跟踪列表中发生了什么变化,以及当数据发生变化时他们正在查看列表中的哪个位置。

Since I've been developing on Android I've found no equivalent facility to animate individual rows in a TableView. Calling notifyDataSetChanged() on my Adapter causes the ListView to immediately update its content with new information. I'd like to show a simple animation of a new row pushing in or sliding out when the data changes, but I can't find any documented way to do this. It looks like LayoutAnimationController might hold a key to getting this to work, but when I set a LayoutAnimationController on my ListView (similar to ApiDemo's LayoutAnimation2) and remove elements from my adapter after the list has displayed, the elements disappear immediately instead of getting animated out.

我还尝试了如下方法,在移除单个项目时将其动画化:

@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
    Animation animation = new ScaleAnimation(1, 1, 1, 0);
    animation.setDuration(100);
    getListView().getChildAt(position).startAnimation(animation);
    l.postDelayed(new Runnable() {
        public void run() {
            mStringList.remove(position);
            mAdapter.notifyDataSetChanged();
        }
    }, 100);
}

但是,动画行周围的行不会移动位置,直到调用notifyDataSetChanged()时它们跳转到新位置。一旦元素被放置,ListView似乎不会更新它的布局。

虽然写我自己的ListView的实现/分支已经越过我的脑海,这似乎不应该是那么困难的事情。

谢谢!


当前回答

你考虑过向右移动吗?您可以在列表项的顶部绘制一个逐渐增大的白色条,然后将其从列表中删除。其他细胞仍然会原地跳动,但总比没有强。

其他回答

我还没有尝试过,但它看起来像animateLayoutChanges应该做什么,你正在寻找。我在ImageSwitcher类中看到了它,我想它也在ViewSwitcher类中?

由于Android是开源的,你实际上不需要重新实现ListView的优化。你可以抓取ListView的代码,并试图找到一种方法来破解动画,你也可以在android漏洞跟踪器中打开一个功能请求(如果你决定实现它,不要忘记贡献一个补丁)。

仅供参考,ListView的源代码在这里。

Animation anim = AnimationUtils.loadAnimation(
                     GoTransitApp.this, android.R.anim.slide_out_right
                 );
anim.setDuration(500);
listView.getChildAt(index).startAnimation(anim );

new Handler().postDelayed(new Runnable() {

    public void run() {

        FavouritesManager.getInstance().remove(
            FavouritesManager.getInstance().getTripManagerAtIndex(index)
        );
        populateList();
        adapter.notifyDataSetChanged();

    }

}, anim.getDuration());

对于自上而下的动画使用:

<set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromYDelta="20%p" android:toYDelta="-20"
            android:duration="@android:integer/config_mediumAnimTime"/>
        <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_mediumAnimTime" />
</set>

你考虑过向右移动吗?您可以在列表项的顶部绘制一个逐渐增大的白色条,然后将其从列表中删除。其他细胞仍然会原地跳动,但总比没有强。

RecyclerView负责添加、删除和重新排序动画!

这个简单的AndroidStudio项目有一个RecyclerView。看一下提交:

提交经典的Hello World Android应用程序 commit,添加一个RecyclerView到项目中(内容不是动态的) commit,增加了在运行时修改RecyclerView内容的功能(但没有动画) 最后…提交添加动画到RecyclerView