Created
November 27, 2022 05:47
-
-
Save shibbirweb/6eb55599652283d0d896cab0d4a912a7 to your computer and use it in GitHub Desktop.
Andorid - Kotlin - RecyclerView View binding adapter with memory leak fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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