Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// activity
private val onAppBarLiftListeners = HashSet<AppBarLiftListener>()
val appBar = findViewById<AppBarLayout>(R.id.appBar)
if (appBar != null) {
appBar.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { _, verticalOffset ->
onAppBarLiftListeners.forEach { it.onAppBarLift(verticalOffset) }
})
}
fun addOnAppBarLiftListener(listener: AppBarLiftListener) = onAppBarLiftListeners.add(listener)
fun removeOnAppBarLiftListener(listener: AppBarLiftListener) = onAppBarLiftListeners.remove(listener)
// file
interface AppBarLiftListener {
fun onAppBarLift(offset: Int)
}
// base fragment
addOnAppBarLiftListener(this@BaseFragment)
removeOnAppBarLiftListener(this@BaseFragment)
override fun onAppBarLift(offset: Int) {
}
// some fragment, but need to move to base
override fun onAppBarLift(offset: Int) {
val y = -((147 / 2).toFloat() + offset.toFloat())
if (y < 0) {
binding.progressRetry.progress.translationY = y
binding.progressRetry.errorLayout.view.translationY = y
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment