Skip to content

Instantly share code, notes, and snippets.

@ajoydas
Created December 16, 2017 09:25
Show Gist options
  • Save ajoydas/c08a6c62a307b1b6a08b06a902ff8196 to your computer and use it in GitHub Desktop.
Save ajoydas/c08a6c62a307b1b6a08b06a902ff8196 to your computer and use it in GitHub Desktop.
private void setupTabs() {
mTabHost = (MaterialTabHost) findViewById(R.id.materialTabHost);
mPager = (ViewPager) findViewById(R.id.viewPager);
mAdapter = new ViewPagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mAdapter);
//when the page changes in the ViewPager, update the Tabs accordingly
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
mTabHost.setSelectedNavigationItem(position);
}
});
//Add all the Tabs to the TabHost
for (int i = 0; i < mAdapter.getCount(); i++) {
mTabHost.addTab(
mTabHost.newTab()
.setText(mAdapter.getPageTitle(i))
.setTabListener(this));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment