Skip to content

Instantly share code, notes, and snippets.

@Alec-DeSouza
Last active May 31, 2018 01:21
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 Alec-DeSouza/0b4577ccfc2571147703392149030b6f to your computer and use it in GitHub Desktop.
Save Alec-DeSouza/0b4577ccfc2571147703392149030b6f to your computer and use it in GitHub Desktop.
ViewPager and TabLayout Dynamic Tab Opacity Interpolation [Kotlin]
view_pager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
// Interpolate all tab opacity values
for (index in 0 until tab_layout.tabCount) {
tab_layout.getTabAt(index)?.updateOpacity(position, positionOffset)
}
}
fun TabLayout.Tab.updateOpacity(position: Int, offset: Float) {
val diffTab = 1 - abs(position - this.position + offset)
// Update opacity of containing elements
customView?.alpha = 0.5f + if (diffTab in 0.0..1.0) (diffTab * 0.5).toFloat() else 0f
icon?.alpha = 127 + if (diffTab in 0.0..1.0) (diffTab * 128).toInt() else 0
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment