[Android/kotlin] Cannot call this method in a scroll callback

JINA·2021년 8월 31일
0

Android/error

목록 보기
1/1

🚨Cannot call this method in a scroll callback


W/RecyclerView: Cannot call this method in a scroll callback. Scroll callbacks mightbe run during a measure & layout pass where you cannot change theRecyclerView data. Any method call that might change the structureof the RecyclerView or the adapter contents should be postponed tothe next frame. 
java.lang.IllegalStateException:  androidx.recyclerview.widget.RecyclerView{bcfafdd VFED..... ......ID 0,0-340,640 #7f0a0146 app:id/recyclerView}, adapter:com.example.getcontents.adapter.RecyclerViewAdapter@f16752, layout:androidx.recyclerview.widget.LinearLayoutManager@fb02a23, context:com.example.getcontents.activity.ContentsListActivity@7fec6f9

스크롤 콜백에서 이 메서드를 호출할 수 없습니다. 스크롤 콜백은 RecyclerView 데이터를 변경할 수 없는 측정 및 레이아웃 단계에서 실행될 수 있습니다. RecyclerView 또는 어댑터 내용의 구조를 변경할 수 있는 모든 메서드 호출은 다음 프레임으로 연기되어야 함.

📌오류원인

  • 리사이클러뷰를 스크롤 하는 동안 notify 관련 함수(notifyDataSetChanged, notifyItemChanged 등)를 사용했기 때문

🙌해결방법

val runnable = Runnable {
            items.add(null)
            mListAdapter.notifyItemInserted(items.size -1)
        }
        mRecyclerView.post(runnable)

참고사이트

0개의 댓글