2016-09-08 84 views
0

當我在RecyclerView中刪除了一些項目,但它涉及到一個bug時,錯誤很難發生,但它有時會發生。有時該項目不會被刪除,並仍然顯示在recyclerView中,無法永久刪除。以下是我的代碼:recyclerView在循環bug中notifyItemRemoved?

//the positions is a collection,which collect my item positions that should be removed. 

int realPosition; 
int temp = 0; 
for (int i = 0; i < positions.size(); i++) { 
    realPosition = positions.get(i) - temp; 
    getAdapter().remove(realPosition); 
    getAdapter().notifyItemRemoved(realPosition); 
    temp++; 
} 

我的英文很差,希望你能理解。誰能幫我?非常感謝你。

回答

0

如果你想在循環中刪除從回收站視圖中的項目,然後嘗試這個

int realPosition; 
int temp = 0; 
for (int i = 0; i < positions.size(); i++) { 
    realPosition = positions.get(i) - temp; 
    getAdapter().remove(realPosition); 
    temp++; 
} 
getAdapter().notifyDataSetChanged(); 

與您的代碼的問題是,每次你的循環運行recyclerView的立場得到更新,以便您的notifyItemRemoved(realPosition)被移除除了你真正想要的東西....希望這可以幫助

+0

THX非常! (int i = positions.size() - 1:i> = 0; i - )' –

+0

如果這有助於您考慮upvoting或將我的答案標記爲答案 – bharath