Created
December 2, 2019 19:04
-
-
Save nikhilpanju/e1154285a9bd17dbc64cafec49903e75 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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