Tabs Below Action Bar
I'm developing for Android 4.3 and been having a problem with my code that I can't seem to figure out. I've been looking for answers for a while and all I could find was people tha
Solution 1:
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
privatestaticfinalintNUM_PAGES=3;
in onCreate()
finalActionBaractionBar= getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = newScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(newViewPager.SimpleOnPageChangeListener() {
@OverridepublicvoidonPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
ActionBar.TabListenertabListener=newActionBar.TabListener() {
@OverridepublicvoidonTabReselected(Tab tab,
android.app.FragmentTransaction ft) {
}
@OverridepublicvoidonTabSelected(Tab tab,
android.app.FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
}
@OverridepublicvoidonTabUnselected(Tab tab,
android.app.FragmentTransaction ft) {
}
};
for (inti=0; i < mPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar
.newTab()
.setText(mPagerAdapter.getPageTitle(i))
.setIcon(
((ScreenSlidePagerAdapter) mPagerAdapter)
.getPageIcon(i))
.setTabListener(tabListener));
}
privateclassScreenSlidePagerAdapterextendsFragmentStatePagerAdapter {
publicScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Overridepublic Fragment getItem(int position) {
switch (position) {
case0:
return Fragment.instantiate(MainActivity.this,
Fragment_sports.class.getName());
case1:
return Fragment.instantiate(MainActivity.this,
Fragment_casino.class.getName());
case2:
return Fragment.instantiate(MainActivity.this,
Fragment_live_betting.class.getName());
default:
break;
}
returnnull;
}
@OverridepublicintgetCount() {
return NUM_PAGES;
}
@Overridepublic CharSequence getPageTitle(int position) {
StringtabLabel=null;
switch (position) {
case0:
tabLabel = " Sports";
break;
case1:
tabLabel = "Casino";
break;
case2:
tabLabel = "Live Betting";
break;
}
return tabLabel;
}
publicintgetPageIcon(int position) {
intid=0;
switch (position) {
case0:
id = R.drawable.icon_all_sports_d;
break;
case1:
id = R.drawable.icon_favourites_d;
break;
case2:
id = R.drawable.icon_live_d;
break;
default:
break;
}
return id;
}
}
and your main_activity.xml:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent" ><android.support.v4.view.ViewPagerandroid:id="@+id/pager"android:layout_width="wrap_content"android:layout_height="wrap_content" /></RelativeLayout>
ADD:
You should replace this line:
actionBar.setDisplayHomeAsUpEnabled(true);
to this:
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
Solution 2:
Edit: For tabs, use this part of the API guides.
For adding elements to your ActionBar
, you must Override onCreateOptionsMenu()
For example:
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action barMenuInflaterinflater= getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
returnsuper.onCreateOptionsMenu(menu);
}
And you should have an xml for the layout of those items:
res/menu/main_activity_actions.xml
<menuxmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:id="@+id/action_search"android:icon="@drawable/ic_action_search"android:title="@string/action_search"/><itemandroid:id="@+id/action_compose"android:icon="@drawable/ic_action_compose"android:title="@string/action_compose" /></menu>
Post a Comment for "Tabs Below Action Bar"