Created
November 18, 2015 13:43
-
-
Save anonymous/b43882513f5d6923008a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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