2017-02-15 31 views
0

我沒有在RecyclerView上定製任何內容,所以添加/刪除了項目上的默認動畫。雖然我發現動畫不能按我的預期工作。當我用下面的代碼刪除項目:從RecyclerView中刪除項目效果不佳

mComments.remove(position); 
notifyItemRemoved(position); 

我在UI上看到它總是刪除錯誤的,而應該被刪除已經不斷顯示出來,並覆蓋其他人。

new CountDownTimer(60000, 1000) { 
    @Override 
    public void onTick(long l) { 
     for (int i = 0; i < mComments.size(); i++) { 
      RoomMessage item = mComments.get(i); 
      item.timeRemaining -= 1000; 

      if (item.timeRemaining <= 0) { 
       Log.v(TAG, "Going to remove no." + i + ". And the content = " + mComments.get(i).text); 
       removeAt(i); 
      } 
     } 
    } 

    @Override 
    public void onFinish() { 
     start(); 
    } 
}.start(); 

根據日誌,我沒有刪除正確的。如下所示。

02-16 15:26:38.274 21861-21861/com.xxxx.android V/ChatsAdapter: Going to remove no.0. And the content = 1 
02-16 15:26:41.284 21861-21861/com.xxxx.android V/ChatsAdapter: Going to remove no.0. And the content = 2 
02-16 15:26:42.284 21861-21861/com.xxxx.android V/ChatsAdapter: Going to remove no.0. And the content = 3 

我做錯了什麼?

順便說一句我已經設置setisRecyclable(false)

回答

0

僅使用notifyItemRemoved(position);。你同時使用。 notifyItemRemoved這是用於通知以前位於位置的項目已從數據集中刪除。雖然notifyItemRangeChanged(int positionStart, int itemCount)僅在通知任何註冊的觀察者時使用從位置positionStart開始的itemCount項已更改。如果您要添加單個項目,請使用notifyItemInserted。如果您添加了多個新項目,請使用notifyItemRangeInserted(int positionStart, int itemCount)。任何以更改結尾的方法指出該特定項目或行的值已更改。

+0

@Dayo Choul有沒有更新? –

+0

已更新。這很奇怪,根據日誌我刪除了正確的一個,而在屏幕上它總是刪除錯誤的... –

+0

@Dayo Choul所以我的回答沒有幫助你嗎?你也可以發佈日誌。我回答動畫。 –