如何动画添加或删除Android的ListView的行 [英] How to Animate Addition or Removal of Android ListView Rows

查看:167
本文介绍了如何动画添加或删除Android的ListView的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS系统中,有一个非常简单而功能强大的工具,以动画的UITableView的行添加和删除,这里有来自YouTube视频剪辑显示默认的动画。注意周围的行是如何崩溃到删除的行。这个动画可以帮助用户跟踪列表中有什么改变,即列表中的他们看的时候,数据变化。

由于我一直在开发Android上,我发现没有相应的设施,以在 TableView中。调用<一href="http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged%28%29"><$c$c>notifyDataSetChanged()在我的适配器导致的ListView立即更新其内容与新的信息。我想表明一个新行的一个简单的动画推或滑动出来的数据发生变化时,但我找不到任何记录的方式来做到这一点。它看起来像 LayoutAnimationController 可能抓住重点,以得到这个工作,但是当我把我的ListView一个LayoutAnimationController(类似于<一href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/LayoutAnimation2.html">ApiDemo's LayoutAnimation2 )和名单显示后删除我的适配器的元素,这些元素立刻消失入门动画出来的吧。

我也试着喜欢的东西下面的动画被删除,当一个人的项目:

  @覆盖
保护无效onListItemClick(ListView的L,视图V,最终诠释的位置,长的id){
    动画动画=新ScaleAnimation(1,1,1,0);
    animation.setDuration(100);
    。getListView()getChildAt(位置).startAnimation(动画);
    l.postDelayed(新的Runnable(){
        公共无效的run(){
            mStringList.remove(位置);
            mAdapter.notifyDataSetChanged();
        }
    },100);
}
 

然而,围绕动画行的行不动的位置,直到他们跳到新的位置时, notifyDataSetChanged()被调用。看来一旦其内容已被放置ListView控件不更新其布局。

在写我自己的实现/ ListView控件的叉子已经越过我的脑海里,这似乎喜欢的事,不应该这么难。

谢谢!

解决方案

 动画动画= AnimationUtils.loadAnimation(
                     GoTransitApp.this,android.R.anim.slide_out_right
                 );
anim.setDuration(500);
listView.getChildAt(指数).startAnimation(阿尼姆);

新的处理程序()。postDelayed(新的Runnable(){

    公共无效的run(){

        FavouritesManager.getInstance()。删除(
            FavouritesManager.getInstance()。getTripManagerAtIndex(指数)
        );
        populateList();
        adapter.notifyDataSetChanged();

    }

},anim.getDuration());
 

有关上到下的动画使用:

 &LT;设置的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;
        &LT;翻译机器人:fromYDelta =20%P机器人:toYDelta = -  20
            机器人:时间=@安卓整数/ config_mediumAnimTime/&GT;
        &LT;阿尔法机器人:fromAlpha =0.0的Andr​​oid版本:toAlpha =1.0
            机器人:时间=@安卓整数/ config_mediumAnimTime/&GT;
&LT; /集&gt;
 

In iOS, there is a very easy and powerful facility to animate the addition and removal of UITableView rows, here's a clip from a youtube video showing the default animation. Note how the surrounding rows collapse onto the deleted row. This animation helps users keep track of what changed in a list and where in the list they were looking at when the data changed.

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.

I've also tried things like the following to animate an individual item when it is removed:

@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);
}

However, the rows surrounding the animated row don't move position until they jump to their new positions when notifyDataSetChanged() is called. It appears ListView doesn't update its layout once its elements have been placed.

While writing my own implementation/fork of ListView has crossed my mind, this seems like something that shouldn't be so difficult.

Thanks!

解决方案

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());

for top-to-down animation use :

<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>

这篇关于如何动画添加或删除Android的ListView的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆