material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.37k stars 3.07k forks source link

[TabLayoutMediator] Extend implementation with custom ViewPager2.OnPageChangeCallback #823

Closed daviddebre closed 2 years ago

daviddebre commented 4 years ago

TabLayoutMediator with the use of TabLayoutOnPageChangeCallback triggers scrolling the tab indicator in onPageScrolled (via updateIndicator). This implementations doesn't let to disable the tab indicator scrolling.

The solution would be to extend the TabLayoutMediator in order to enable/disable tab indicator scrolling. Another potential solution is to be support setting a custom ViewPager2.OnPageChangeCallback/TabLayoutOnPageChangeCallback for the onPageChangeCallback field.

Version of library: 1.2.0-alpha02

Similar feature request: https://github.com/material-components/material-components-android/issues/813

If the feature request is approved, then I could help with an appropriate pull request.

drchen commented 3 years ago

Can you state the reason why you need to disable the indicator scrolling?

daviddebre commented 3 years ago

Actually this was a request from the client, the app was built for.

By the way, here is the solution which was implemented to disable scrolling:

class ImageCarouselTabLayout(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : TabLayout(context, attrs, defStyleAttr) {

    override fun setScrollPosition(position: Int, positionOffset: Float, updateSelectedText: Boolean, updateIndicatorPosition: Boolean) {
        val manipulatedUpdateIndicatorPosition =
            if (positionOffset == 0f) {
                updateIndicatorPosition
            } else {
                /**
                 * since [positionOffset] represents the value of scrolling between two tab: updating the scrolling fraction for tabIndicator
                 * can be avoided by not updating indicator position (setting false) when [positionOffset] is not 0
                 */
                false
            }

        super.setScrollPosition(position, positionOffset, updateSelectedText, manipulatedUpdateIndicatorPosition)
    }
}
drchen commented 2 years ago

Does https://github.com/material-components/material-components-android/commit/a295de97283b51cacfbc74e58e95048d348cc471 serve your need?

drchen commented 2 years ago

I'll close this issue for now due to no further info available. Please feel free to reopen it if https://github.com/material-components/material-components-android/commit/a295de97283b51cacfbc74e58e95048d348cc471 doesn't solve your issue. : )