My HomeActivity extends AppCompatActivity that uses 2 tabs.
public class HomeActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private TabLayout tabLayout;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
...
How to listen to tab change event? Let me know if I need to add any more code for clarity.
You can use OnTabChangeListener.See below
TabLayout tabLayout = new TabLayout(this);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
//do stuff here
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
hope it help.