Skip to content

Instantly share code, notes, and snippets.

@TalbotGooday
Created April 29, 2019 10:46
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 TalbotGooday/531681644ca4da315ba5240fb1587b7c to your computer and use it in GitHub Desktop.
Save TalbotGooday/531681644ca4da315ba5240fb1587b7c to your computer and use it in GitHub Desktop.
Allows you to smoothly hide the View
fun RecyclerView.attachScrollToFade(view: View? = null) {
addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val linearLayoutManager = layoutManager as LinearLayoutManager
if (linearLayoutManager.findFirstVisibleItemPosition() == 0) {
val firstVisible = linearLayoutManager.findViewByPosition(linearLayoutManager.findFirstVisibleItemPosition())
if (firstVisible != null) {
val distanceTop = firstVisible.top
val itemSize = firstVisible.height + (firstVisible.layoutParams as ViewGroup.MarginLayoutParams).bottomMargin // view size
val alpha = (recyclerView.bottom - recyclerView.paddingBottom - distanceTop) / (itemSize.toFloat()) // view transparency
view?.alpha = alpha
}
}else{
view?.alpha = 0f
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment