Created
April 29, 2019 10:46
-
-
Save TalbotGooday/531681644ca4da315ba5240fb1587b7c to your computer and use it in GitHub Desktop.
Allows you to smoothly hide the View
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
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