我已经使用名为TabHostFragment
的片段内的操作栏实现了标签public class TabHostFragment extends Fragment {
public TabHostFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState == null){
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab_host, null, false);
ViewPager mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
ActionBar actionbar = this.getActivity().getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionbar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
TabsAdapter mTabsAdapter = new TabsAdapter(this, mViewPager);
mTabsAdapter.addTab(actionbar.newTab().setText("Summoner"),
SummonerFragment.class, null);
mTabsAdapter.addTab(actionbar.newTab().setText("Games"),
GameHistoryListFragment.class, null);
mTabsAdapter.addTab(actionbar.newTab().setText("League"),
LeagueFragment.class, null);
mTabsAdapter.addTab(actionbar.newTab().setText("Masteries"),
MasteriesFragment.class, null);
mTabsAdapter.addTab(actionbar.newTab().setText("Runes"),
RunesFragment.class, null);
return rootView;
}
}`
TabsAdapter类在这里
public class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(Fragment frag, ViewPager pager) {
super(frag.getChildFragmentManager());
frag.getChildFragmentManager();
mContext = frag.getActivity();
mActionBar = frag.getActivity().getActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
主要活动包含一个NavigationDrawer,它将TabHostFragment加载到活动
中 if(position == 1){
fragment = new TabHostFragment();
FragmentManager fragmentManager = this.getSupportFragmentManager();
if(!fragmentManager.findFragmentById(R.id.list_container).getClass().equals(TabHostFragment.class)){
fragmentManager.beginTransaction().replace(R.id.list_container, fragment).addToBackStack(null).commit();
if(mTwoPane){
FrameLayout fl = (FrameLayout) findViewById(R.id.detail_container);
fl.removeAllViews();
findViewById(R.id.detail_container).setVisibility(View.GONE);
}
}
}
当离开此片段并返回时,即在导航抽屉中选择其他内容,然后按回或在抽屉中选择TabHostFragment时,标签标题会重复。无法发布图片,因此
我不确定为什么会这样,并且无法在stackoverflow上找到类似的问题。
答案 0 :(得分:5)
当您重新进入特定视图时,似乎没有从ActionBar中清除选项卡。
在添加更多标签之前尝试使用mActionBar.RemoveAllTabs()
。或许在TabsAdapter的构造函数中。