Wednesday, 11 September 2013

How do I get tabs to appear in an ActionBar when using ActionBarDrawerToggle?

How do I get tabs to appear in an ActionBar when using ActionBarDrawerToggle?

I have a FragmentActivity (HomeFragment) that utilizes NavigationDrawer
and ActionBarDrawerToggle. When I run the emulator with this
FragmentActivity as the first displayed FragmentActivity to the user, I
see the ActionBar and the NavigationDrawer works perfectly. From here I am
able to select a menu item ("Account") which takes me to an
AccountFragment. My AccountFragment activity uses a ViewPager to swipe
between a fragment for basic account info and another fragment for a list
of buddies/contacts. My BuddiesFragment is supposed to add navigation tabs
to the actionbar to select buddies from a list, from groups, or from
favorites. I cannot get these tabs to appear under my action bar.
It is worth mentioning that if I start my emulator directly accessing the
BuddiesFragment and skipping navigating to this fragment from my
HomeFragment, the navigation tabs appear underneath the actionbar as
expected. I can't figure out the difference that causes one usecase to
fail but not the other.
I do not have enough 'reputation points' to post picture for clarification.
public class BuddiesFragment extends AbstractBaseFragment implements
ActionBar.TabListener {
private final String[] buddyTabs = new String[] {"Buddy List", "Buddy
Groups", "Buddy Favorites"};
private final String TAG = "BuddiesFragment";
BuddyListAdapter adapter;
NonScrollablePager pager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//getActivity().getActionBar().hide();
View view = inflater.inflate(R.layout.buddies_info_layout, container,
false);
adapter = new BuddyListAdapter(getFragmentManager());
pager = (NonScrollablePager) view.findViewById(R.id.scroll_pager);
pager.setAdapter(adapter);
final ActionBar actionBar = getActivity().getActionBar();
// Specify that the Home/Up button should not be enabled, since there
is no hierarchical
// parent.
actionBar.setHomeButtonEnabled(false);
// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
pager.setOnPageChangeListener(new
ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between different app sections, select the
corresponding tab.
// We can also use ActionBar.Tab#select() to do this if we
have a reference to the
// Tab.
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < adapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined
by the adapter.
// Also specify this Activity object, which implements the
TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(adapter.getPageTitle(i))
.setTabListener(this));
}
return view;
}
The AccountFragment has a ViewPager that allows me to swipe to
AccountInfoFragment and BuddiesFragment. It does not manipulate the
ActionBar whatsoever.
public class AccountFragment extends AbstractBaseFragment {
private CirclePageIndicator pageIndicator;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//getActivity().getActionBar().hide();
View view = inflater.inflate(R.layout.account_layout, container, false);
AccountPagerAdapter apa = new AccountPagerAdapter(getFragmentManager());
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.pager);
mViewPager.setAdapter(apa);
pageIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
pageIndicator.setViewPager(mViewPager);
return view;
}

No comments:

Post a Comment