Skip to content

Instantly share code, notes, and snippets.

@kakajika
Created March 9, 2020 09:38
Show Gist options
  • Save kakajika/1c5ac32dc6f0d13adfb79849acf233b4 to your computer and use it in GitHub Desktop.
Save kakajika/1c5ac32dc6f0d13adfb79849acf233b4 to your computer and use it in GitHub Desktop.
NavigationView with OnNavigationItemReselectedListener like BottomNavigationView.
class ReselectAwareNavigationView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : NavigationView(context, attrs, defStyleAttr) {
interface OnNavigationItemReselectedListener {
fun onNavigationItemReselected(item: MenuItem)
}
private var selectedListener: OnNavigationItemSelectedListener? = null
private var reselectedListener: OnNavigationItemReselectedListener? = null
init {
super.setNavigationItemSelectedListener { item ->
if (reselectedListener != null && item.isChecked) {
reselectedListener?.onNavigationItemReselected(item)
true
} else {
selectedListener?.onNavigationItemSelected(item) ?: false
}
}
}
override fun setNavigationItemSelectedListener(listener: OnNavigationItemSelectedListener?) {
selectedListener = listener
}
fun setNavigationItemReselectedListener(listener: OnNavigationItemReselectedListener?) {
reselectedListener = listener
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment