gpt4 book ai didi

android-fragments - 重复的标签标题 android actionbar 片段

转载 作者:行者123 更新时间:2023-12-02 04:41:21 25 4
gpt4 key购买 nike

我在名为 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,选项卡标题会重复。还不能发图片所以enter image description here

我不确定为什么会这样,并且在 stackoverflow 上的任何地方都找不到类似的问题。

最佳答案

当您重新进入您的特定 View 时,选项卡似乎没有从 ActionBar 中清除。

在添加更多选项卡之前尝试使用 mActionBar.RemoveAllTabs()。也许在您的 TabsAdapter 的构造函数中。

关于android-fragments - 重复的标签标题 android actionbar 片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20777485/

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