如何對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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章