我有一个使用 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;
}