Skip to content

Instantly share code, notes, and snippets.

@tiwiz
Created December 9, 2017 22:31
Show Gist options
  • Save tiwiz/cbb976281af76f6836ef493b003be6c0 to your computer and use it in GitHub Desktop.
Save tiwiz/cbb976281af76f6836ef493b003be6c0 to your computer and use it in GitHub Desktop.
class DimOnScrollObserver : OnScrollChangeObserver() {
private lateinit var target: AppCompatActivity
fun attachTo(v: ScrollableViewCompat) {
v.addOnScrollChangeListener(this)
target = v.getAttachedActivity()
}
fun attachTo(v: RecyclerView, host: AppCompatActivity) {
v.addOnScrollListener(this)
target = host
}
override fun onScrollChange(v: View,
scrollX: Int,
scrollY: Int,
oldScrollX: Int,
oldScrollY: Int) {
onScrollingInvoked(scrollY)
}
private fun onScrollingInvoked(trigger: Int) {
if (trigger > 0) {
invokeDimming()
} else if (trigger == 0) {
revokeDimming()
}
}
override fun invokeDimming() {
target.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE
}
override fun revokeDimming() {
target.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
}
override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
val pos = (recyclerView?.layoutManager as LinearLayoutManager)
.findFirstCompletelyVisibleItemPosition()
onScrollingInvoked(pos)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment