gpt4 book ai didi

java - 让 Actionbar 标签贴在 Fragment 上

转载 作者:行者123 更新时间:2023-11-30 03:11:01 24 4
gpt4 key购买 nike

我目前有一个 MainActivity.java,它应该是唯一的 Activity 类。虽然在那个 Activity 类中我有一个抽屉导航链接到其他 fragment View 。

目前我面临的主要问题是在 fragment 下实现选项卡并使它们仅可用于该 fragment 和子 fragment 。我运行了我的应用程序并出现了选项卡,但在我访问 TeamsAndDriversFragment 后它们也出现在其他 fragment 上。

在我的 MainActivity.java 中,我有以下函数,它有助于指向当有人在抽屉导航中单击它们时它将生成的 fragment :

/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new TimeAndScoringFragment();
break;
case 1:
fragment = new ScheduleFragment();
break;
case 2:
fragment = new StandingsFragment();
break;
case 3:
fragment = new TeamsAndDriversFragment();
break;
case 4:
fragment = new NewsFragment();
break;
default:
break;
}

if (fragment != null) {

// Create a fragment transaction object to be able to switch fragments
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);

} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}

这是我当前的 TeamsAndDriversFragment 类,其中我有一个带有选项卡的操作栏导航:

public class TeamsAndDriversFragment extends Fragment implements TabListener {

private List<Fragment> fragList = new ArrayList<Fragment>();

@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);

ActionBar bar = getActivity().getActionBar();

bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

Tab mTeamsTab = bar.newTab();
mTeamsTab.setText("Teams");
mTeamsTab.setTabListener(this);
bar.addTab(mTeamsTab);

Tab mDriversTab = bar.newTab();
mDriversTab.setText("Drivers");
mDriversTab.setTabListener(this);
bar.addTab(mDriversTab);
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Fragment f = null;
TabFragment tf = null;

if(fragList.size() > tab.getPosition()) {
fragList.get(tab.getPosition());
}

if(f == null) {
tf = new TabFragment();
Bundle data = new Bundle();
data.putInt("idx", tab.getPosition());
tf.setArguments(data);
fragList.add(tf);
} else {
tf = (TabFragment) f;
}

ft.replace(android.R.id.content, tf);
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if(fragList.size() > tab.getPosition()) {
ft.remove(fragList.get(tab.getPosition()));
}
}


}

最佳答案

displayView() 方法中,只需从 ActionBar 中删除所有选项卡,这样您将始终拥有一个干净的 ActionBar TeamsAndDriversFragment fragment 的异常:

private void displayView(int position) {
getSupportActionBar().removeAllTabs();
// ...
}

关于java - 让 Actionbar 标签贴在 Fragment 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20981626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com