在片段之间切换期间,操作栏选项卡会重复

时间:2014-08-19 06:25:36

标签: android android-fragments android-actionbar-compat

导航抽屉包含4个项目,每个项目包含4个操作栏选项卡,而在菜单项目之间切换意味着如果我单击菜单项目2,则操作栏项目数量将更改为8,如果我单击菜单项目3,则操作栏选项卡的数量会发生变化到12,如何停止tabs的重复。这是我的代码

public class TopicsFragment extends Fragment {

public TopicsFragment() {
}

// Declare Tab Variable
    ActionBar.Tab AllTopics, NewContent, StaffPicks, Popular, Recommended;
    Fragment fragmentTab1 = new FragmentTab1();
    Fragment fragmentTab2 = new FragmentTab2();
    Fragment fragmentTab3 = new FragmentTab3();
    Fragment fragmentTab4 = new FragmentTab2();
    Fragment fragmentTab5 = new FragmentTab1();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup tabs,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_topics, tabs, false);

        ActionBar actionBar = ((ActionBarActivity) getActivity())
                .getSupportActionBar();


        // Create Actionbar Tabs
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set Tab Icon and Titles
        AllTopics = actionBar.newTab().setIcon(R.drawable.tab1);
        NewContent = actionBar.newTab().setIcon(R.drawable.tab1);
        StaffPicks = actionBar.newTab().setIcon(R.drawable.tab1);
        Popular = actionBar.newTab().setIcon(R.drawable.tab1);
        Recommended = actionBar.newTab().setIcon(R.drawable.tab1);
        // Set Tab Listeners
        AllTopics.setTabListener(new TabListener(fragmentTab1));
        NewContent.setTabListener(new TabListener(fragmentTab2));
        StaffPicks.setTabListener(new TabListener(fragmentTab3));
        Popular.setTabListener(new TabListener(fragmentTab2));
        Recommended.setTabListener(new TabListener(fragmentTab2));

        // Add tabs to actionbar
        actionBar.addTab(AllTopics);
        actionBar.addTab(NewContent);
        actionBar.addTab(StaffPicks);
        actionBar.addTab(Popular);
        actionBar.addTab(Recommended);
        return rootView;
    }

}

1 个答案:

答案 0 :(得分:0)

问题是,当您返回TopicsFragment时,您会不断添加标签,这会再次拨打onCreateView并执行adding of tabs操作栏从而在当前标签中添加另一组标签。

<强>溶液

如果操作栏中的标签数量为零,则需要先检查。

<强>样品:

actionBar.removeAllTabs();
if(actionBar.getTabCount() == 0)
    {
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set Tab Icon and Titles
        AllTopics = actionBar.newTab().setIcon(R.drawable.tab1);
        NewContent = actionBar.newTab().setIcon(R.drawable.tab1);
        StaffPicks = actionBar.newTab().setIcon(R.drawable.tab1);
        Popular = actionBar.newTab().setIcon(R.drawable.tab1);
        Recommended = actionBar.newTab().setIcon(R.drawable.tab1);
        // Set Tab Listeners
        AllTopics.setTabListener(new TabListener(fragmentTab1));
        NewContent.setTabListener(new TabListener(fragmentTab2));
        StaffPicks.setTabListener(new TabListener(fragmentTab3));
        Popular.setTabListener(new TabListener(fragmentTab2));
        Recommended.setTabListener(new TabListener(fragmentTab2));

        // Add tabs to actionbar
        actionBar.addTab(AllTopics);
        actionBar.addTab(NewContent);
        actionBar.addTab(StaffPicks);
        actionBar.addTab(Popular);
        actionBar.addTab(Recommended);
    }