如何对Android ListView行的添加或删除进行动画处理 - How to Animate Addition or Removal of Android ListView Rows

问题:

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. 在iOS中,有一个非常简单而强大的工具可以对UITableView行的添加和删除进行动画处理, 这是一个来自youtube视频的剪辑,显示了默认动画。 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 . 自从我在Android上进行开发以来,我发现没有等效的工具可以对TableView中的各个行进行动画处理。 Calling notifyDataSetChanged() on my Adapter causes the ListView to immediately update its content with new information. 在我的适配器上调用notifyDataSetChanged()会使ListView立即用新信息更新其内容。 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. 看起来LayoutAnimationController可能拥有使它起作用的关键,但是当我在ListView上设置LayoutAnimationController(类似于ApiDemo的LayoutAnimation2 )并在列表显示后从适配器中删除元素时,这些元素会立即消失而不是被动画化。

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. 但是,动画行周围的行notifyDataSetChanged()在调用notifyDataSetChanged()时跳到新位置后才移动位置。 It appears ListView doesn't update its layout once its elements have been placed. 放置元素后,ListView似乎不会更新其布局。

While writing my own implementation/fork of ListView has crossed my mind, this seems like something that shouldn't be so difficult. 在编写我自己的ListView的实现/分支时,我已经想到了,但这似乎并不难。

Thanks! 谢谢!


解决方案:

参考一: https://en.stackoom.com/question/GTtx
参考二: https://stackoom.com/question/GTtx
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章