0

I am trying to catch the TabLayout changing when it is either clicked or the user swipes, but unfortunately Ive had little success getting this to work.

I was trying to add a addOnLayoutChangeListener to the OnCreate for my activity that creates the actual TabLayout but with little luck.

A couple of questions:

  1. Is what I am trying to do the correct and best way to do this? I intend to have 3 recycler views that will be alternated between when the user clicks the TabLayout tabs.

  2. Is there a better way to do this?

Thanks again for any information or help.

Jamie
  • 11
  • 3

1 Answers1

0

What you are Looking for is TabLayout.setOnTabSelectedListener()

So basically when you selected or click on any tabs you will get callback. Here is sample code

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

Once you have index you can update you recycler view with different dataset or rather complete dataset.

Code reference from here How to get current selected tab index in TabLayout?

Community
  • 1
  • 1
silentsudo
  • 6,730
  • 6
  • 39
  • 81