Skip to content

Instantly share code, notes, and snippets.

Created November 18, 2015 13:43
Show Gist options
  • Save anonymous/b43882513f5d6923008a to your computer and use it in GitHub Desktop.
Save anonymous/b43882513f5d6923008a to your computer and use it in GitHub Desktop.
private void setupViewPagerAndTabLayout() {
final ViewPager viewPager = (ViewPager) findViewById(R.id.main_view_pager);
viewPager.setAdapter(new MainViewPagerAdapter(getSupportFragmentManager(), this));
TabLayout tabLayout = (TabLayout) findViewById(R.id.main_tab_layout);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabsFromPagerAdapter(viewPager.getAdapter());
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
TabLayout.Tab tab = null;
for(int i = tabLayout.getTabCount() - 1; i >= 0; i--) {
tab = tabLayout.getTabAt(i);
}
tab.select();
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition(), true);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment