Created
December 9, 2017 22:31
-
-
Save tiwiz/cbb976281af76f6836ef493b003be6c0 to your computer and use it in GitHub Desktop.
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
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