개발일지

Android in A..Z - RecyclerView (AdapterDataObserver) 본문

Android (안드로이드)/RecyclerView

Android in A..Z - RecyclerView (AdapterDataObserver)

강태종 2021. 1. 10. 17:34

AdapterDataObserver

RecyclerView.Adapter에서 notifyItem~ 함수를 사용할 때 콜백을 받을 수 있는 Observer이다.

private val adapterDataObserver = object : RecyclerView.AdapterDataObserver() {
	override fun onChanged() {
		super.onChanged()
		Log.d("PASS", "onChanged")
	}

	override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
		super.onItemRangeChanged(positionStart, itemCount)
		Log.d("PASS", "onItemRangeChanged START: $positionStart, COUNT : $itemCount")
	}

	override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
		super.onItemRangeInserted(positionStart, itemCount)
		Log.d("PASS", "onItemRangeInserted POS : $positionStart, COUNT : $itemCount")
	}

	override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
		super.onItemRangeMoved(fromPosition, toPosition, itemCount)
		Log.d("PASS", "onItemRangeMoved FROM : $fromPosition, TO : $toPosition, COUNT : $itemCount")
	}

	override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
		super.onItemRangeRemoved(positionStart, itemCount)
		Log.d("PASS", "onItemRangeRemoved POS : $positionStart, COUNT : $itemCount")
	}
}

ToDoAdapter

private val adapter by lazy { ToDoAdapter().apply { registerAdapterDataObserver(adapterDataObserver) } }

Git (예제코드)

github.com/KangTaeJong98/Example/tree/main/Android/RecyclerView

 

KangTaeJong98/Example

My Example Code. Contribute to KangTaeJong98/Example development by creating an account on GitHub.

github.com

 

Comments