공부방/Android

How to remove Android StaggeredGridLayoutManager item (StaggeredGridLayoutManager item 삭제 방법)

soycrab 2019. 9. 26. 20:12

보통 RecyclerView 에서 아이템 삭제후에 

 

notifyItemRangeRemoved() 또는

 
notifyItemRemoved() 을 이용해서 

 

아이템의 삭제 여부를 통보 해주면 별다른 이상없이 

 

잘 작동 하지만, 

 

StaggeredGridLayoutManager 에서는 

 

각 뷰 타입별로 Span 을 달리 하기 위해 

 

ItemDecoration 을 사용했는데, 

 

여기서 out of index 에러가 나는 것이다. 

 

찾아보니 아래와 같은 순서로 제거 동작을 수행하면 

 

더 이상 에러가 나지 않았다. 

 

val index = arrayList.indexOf(item)
arrayList.removeAt(index)
notifyItemRemoved(index)
notifyItemRangeRemoved(index, 1)

 

 

 

 

 

참고 : https://stackoverflow.com/questions/26076965/android-recyclerview-addition-removal-of-items

반응형