Tab Layout Mediator
A mediator to link a TabLayout with a ViewPager2. The mediator will synchronize the ViewPager2's position with the selected tab when a tab is selected, and the TabLayout's scroll position when the user drags the ViewPager2. TabLayoutMediator will listen to ViewPager2's OnPageChangeCallback to adjust tab when ViewPager2 moves. TabLayoutMediator listens to TabLayout's OnTabSelectedListener to adjust VP2 when tab moves. TabLayoutMediator listens to RecyclerView's AdapterDataObserver to recreate tab content when dataset changes.
Establish the link by creating an instance of this class, make sure the ViewPager2 has an adapter and then call attach on it. Instantiating a TabLayoutMediator will only create the mediator object, attach will link the TabLayout and the ViewPager2 together. When creating an instance of this class, you must supply an implementation of in which you set the text of the tab, and/or perform any styling of the tabs that you require. Changing ViewPager2's adapter will require a detach followed by attach call. Changing the ViewPager2 or TabLayout will require a new instantiation of TabLayoutMediator.