개발새발 - IT 기술블로그
article thumbnail

 

아이템을 삭제하고 갱신하는 로직을 구성하는 도중, 리사이클러뷰의 아이템을 삭제하는 뷰모델과 리스트를 불러오는 뷰모델이 충돌하여 해당 에러가 발생하고 말았습니다. 

 

 

스택 오버플로를 참고하면 서로 다른 스레드에서 돌아가는 뷰모델이 작업을 완료하기 전같은 뷰의 제어를 호출해서 오류가 발생한 것으로 보입니다.

 

스택오버플로 페이지 링크

 

해결방법은 LayoutManager의 Wrapper를 생성하고 애니메이션을 false로 설정해 주면 해당 에러가 사라지게 됩니다.

 

이제 적용방법에 대해 알아보겠습니다.

 

코드

 

먼저 지정할 레이아웃매니저 클래스를 생성합니다.

 

LinearLayoutManagerWrapper

class LinearLayoutManagerWrapper: LinearLayoutManager {
    constructor(context: Context) : super(context)

    constructor(context: Context, orientation: Int, reverseLayout: Boolean) : super(context, orientation, reverseLayout)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int)
            : super(context, attrs, defStyleAttr, defStyleRes)

    override fun supportsPredictiveItemAnimations(): Boolean {
        return false
    }
}

 

 

다음으로 리사이클러뷰에 레이아웃 매니저를 지정합니다.

 

 

방법1 - 뷰 클래스에서 지정

binding.mainRecyclerView.layoutManager = LinearLayoutManagerWrapper(requireActivity())

 

 

방법2 - xml 레이아웃에서 지정

<androidx.recyclerview.widget.RecyclerView
            …
            app:layoutManager=".LinearLayoutManagerWrapper"/>

 

 

다른 방법이나 원인과 해결에 대한 자세한 내용을 아시는분은 답글 남겨주시면 감사드리겠습니다.

 

감사합니다.