Search code examples
androidandroid-layoutandroid-tablayout

How to listen scrolling event in a TabLayout Android


I am using TabLayout in my project which might have more than 10 TabItems. Now I wanted to hide an other view when the user scroll the TabLayout.

Note: I am not using any ViewPager.


Solution

  • TabLayout has its own functionality for tab change listener callback. Use

    onPageChange Listener:

    TabLayout.TabLayoutOnPageChangeListener(TabLayout tabLayout)
    

    Check this link: OnTabChange Listener for more information

    onTabSelected Listener:

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){
        @Override
        public void onTabSelected(TabLayout.Tab tab){
            int position = tab.getPosition();
        }
    });
    

    Check for more: onTabSelected Listener