I'm trying out Android's new TabLayout class to add two tabs just below my ActionBar. Each tab will host a different fragment.
Also, I don't want to be able to swipe between my two tabs - to navigate between my tabs, I'd like to be able to ONLY touch the tab I want to navigate to.
Inside my MainActivity, I have:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setText("Newsfeed"));
tabLayout.addTab(tabLayout.newTab().setText("Random"));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
// IDEALLY HERE, I'd like to do something like
// tab.setFragment(new MainFragment()).
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
So i'd like to override my onTabSelected and onTabReselected methods so that toggling between the two tabs leads to displaying two different fragments respectively. There's not much I could find online about the new TabLayout independent of the ViewPager.
ANy clues? Thanks!