Skip to content

Instantly share code, notes, and snippets.

@nikhilpanju
Created December 2, 2019 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nikhilpanju/e1154285a9bd17dbc64cafec49903e75 to your computer and use it in GitHub Desktop.
Save nikhilpanju/e1154285a9bd17dbc64cafec49903e75 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