I can't use the method setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)
on ActionBar
. I don't understand why my Application doesn't run.
Isn't it supported by the library?
actionBar.setNavigationMode.(ActionBar.NAVIGATION_MODE_TABS);
I can't use the method setNavigationMode(ActionBar.NAVIGATION_MODE_TABS)
on ActionBar
. I don't understand why my Application doesn't run.
Isn't it supported by the library?
actionBar.setNavigationMode.(ActionBar.NAVIGATION_MODE_TABS);
You should use the design support library TabLayout, https://developer.android.com/reference/android/support/design/widget/TabLayout.html. See http://android-developers.blogspot.no/2015/05/android-design-support-library.html from the section Tabs:
Tabs Switching between different views in your app via tabs is not a new concept to material design and they are equally at home as a top level navigation pattern or for organizing different groupings of content within your app (say, different genres of music).
The Design library’s TabLayout implements both fixed tabs, where the view’s width is divided equally between all of the tabs, as well as scrollable tabs, where the tabs are not a uniform size and can scroll horizontally. Tabs can be added programmatically:
TabLayout tabLayout = ...;
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
However, if you are using a ViewPager for horizontal paging between tabs, you can create tabs directly from your PagerAdapter’s getPageTitle() and then connect the two together using setupWithViewPager(). This ensures that tab selection events update the ViewPager and page changes update the selected tab.
Here is mine, you can try it. Hope it's helpful.
First, implements ActionBar.TabListener.
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {}
then, setup in onCreate().
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(adapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
getActionBar().setSelectedNavigationItem(position);
}
});
mPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.page_margin));
getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int position = 0; position < adapter.getCount(); position++) {
getActionBar().addTab(getActionBar().newTab()
.setText(adapter.getPageTitle(position))
.setTabListener(this));
}
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
override other,
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
Rather than actionbar navigation mode, I would like to suggest you to use this library. PagerSlidingTabStrip
Because actionbar navigation mode method supports api level 14 and greater than 14 but this library will support from android 2.3 and higher one.