Skip to content

Instantly share code, notes, and snippets.

@alashow
Created May 24, 2019 22:13
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 alashow/10fbbd933cb9da8e042dcd9544706d04 to your computer and use it in GitHub Desktop.
Save alashow/10fbbd933cb9da8e042dcd9544706d04 to your computer and use it in GitHub Desktop.
// 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