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.
Use the ViewPager.onPageChangeListener
:
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With