使用ActionBarDrawerToggle时,如何在ActionBar中显示标签?

时间:2013-09-12 05:51:00

标签: android tabs android-fragments android-actionbar

我有一个利用NavigationDrawer和ActionBarDrawerToggle的FragmentActivity(HomeFragment)。当我将FragmentActivity作为第一个向用户显示的FragmentActivity运行模拟器时,我看到ActionBar和NavigationDrawer完美地工作。从这里我可以选择一个菜单项(“帐户”),它将我带到AccountFragment。我的AccountFragment活动使用ViewPager在基本帐户信息的片段和伙伴/联系人列表的另一个片段之间滑动。我的BuddiesFragment应该向操作栏添加导航标签,以从列表,组或收藏夹中选择好友。我无法将这些标签显示在我的操作栏下。

值得一提的是,如果我启动模拟器直接访问BuddiesFragment并跳过从HomeFragment导航到此片段,导航选项卡将按预期显示在操作栏下方。我无法弄清楚导致一个用例失败而另一个失败的区别。

我没有足够的'声望点'来张贴图片以供澄清。

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;

}

AccountFragment有一个ViewPager,允许我滑动到AccountInfoFragment和BuddiesFragment。它不会操纵ActionBar。

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;

}
private class AccountPagerAdapter extends FragmentPagerAdapter {
    private BuddiesFragment buddiesFragment;
    private AccountInfoFragment accountInfoFragment;

    private AccountPagerAdapter(FragmentManager fm) {
        super(fm);
        buddiesFragment = new BuddiesFragment();
        accountInfoFragment = new AccountInfoFragment();
    }

    @Override
    public Fragment getItem(int i) {
        switch(i) {
            case 0:
                return accountInfoFragment;

            default:
                return buddiesFragment;
        }
    }

    @Override
    public int getCount() {
        return 2;
    }

0 个答案:

没有答案