Skip to content

Instantly share code, notes, and snippets.

@sabiou
Forked from nikhilpanju/TabsTransformer.kt
Created December 16, 2019 23:44
Show Gist options
  • Save sabiou/a81c3b33bf63419fa8ee28e6b998213e to your computer and use it in GitHub Desktop.
Save sabiou/a81c3b33bf63419fa8ee28e6b998213e to your computer and use it in GitHub Desktop.
tabsRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
totalTabsScroll += dx
}
})
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
// Scroll tabs as viewpager is scrolled
val dx = (position + positionOffset) * tabItemWidth - totalTabsScroll
tabsRecyclerView.scrollBy(dx.toInt(), 0)
// This acts like a page transformer for tabsRecyclerView. Ideally we should do this in the
// onScrollListener for the RecyclerView but that requires extra math. positionOffset
// is all we need so let's use that to apply transformation to the tabs
val currentTabView = tabsRecyclerView.layoutManager?.findViewByPosition(position)!!
val nextTabView = tabsRecyclerView.layoutManager?.findViewByPosition(position + 1)
val defaultScale: Float = FiltersTabsAdapter.defaultScale
val maxScale: Float = FiltersTabsAdapter.maxScale
currentTabView.setScale(defaultScale + (1 - positionOffset) * (maxScale - defaultScale))
nextTabView?.setScale(defaultScale + positionOffset * (maxScale - defaultScale))
currentTabView.findViewById<View>(R.id.tab_pill).backgroundTintList =
ColorStateList.valueOf(blendColors(tabColor, tabSelectedColor, 1 - positionOffset))
nextTabView?.findViewById<View>(R.id.tab_pill)?.backgroundTintList =
ColorStateList.valueOf(blendColors(tabColor, tabSelectedColor, positionOffset))
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment