Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shibbirweb/6eb55599652283d0d896cab0d4a912a7 to your computer and use it in GitHub Desktop.
Save shibbirweb/6eb55599652283d0d896cab0d4a912a7 to your computer and use it in GitHub Desktop.
Andorid - Kotlin - RecyclerView View binding adapter with memory leak fix
@BindingAdapter(
value = ["android:custom_setAdapter", "android:custom_showItemDivider"],
requireAll = false
)
fun RecyclerView.bindRecyclerViewAdapter(
adapter: RecyclerView.Adapter<*>,
showItemDivider: Boolean = false
) {
this.setHasFixedSize(true)
this.adapter = adapter
overScrollMode = View.OVER_SCROLL_NEVER
if (showItemDivider) {
this.addItemDecoration(
DividerItemDecoration(
this.context,
DividerItemDecoration.VERTICAL
)
)
}
this.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(p0: View?) {
}
override fun onViewDetachedFromWindow(p0: View?) {
this@bindRecyclerViewAdapter.adapter = null
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment