【问题标题】:Drawer navigation + tab navigation having too many tabs抽屉导航 + 选项卡导航有太多选项卡
【发布时间】:2015-02-06 08:00:56
【问题描述】:

我正在尝试将抽屉导航和标签导航结合在一起。我已经成功地结合了 2。当我第一次启动该应用程序时,一切正常。但是,当我点击抽屉项目并返回标签片段后,添加的标签太多了。我试图限制数量,但没有奏效。此外,当我打开抽屉项目中的其他片段时,标签仍然存在。

这里有一些图片要展示

当我第一次打开应用程序时

打开抽屉后

当我再次回到第一个片段时

这是我在 createview 上的标签片段的代码

 private ActionBar actionBar;
private String[] tabs={ "1", "2", "3","4" };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.activity_home, container, false);
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getChildFragmentManager());

    mViewPager = (ViewPager) v.findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    // When swiping between pages, select the
                    // corresponding tab.
                    getActivity().getActionBar().setSelectedNavigationItem(position);
                }
            });
    actionBar=getActivity().getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            // show the given tab
            mViewPager.setCurrentItem(tab.getPosition());
        }

        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // hide the given tab
        }

        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // probably ignore this event
        }
    };
    for (String tab_name : tabs) {
         actionBar.addTab(actionBar.newTab().setText(tab_name)
                        .setTabListener(tabListener));
    }

    return v;
}

我认为是

actionBar.addTab(actionBar.newTab().setText(tab_name)
                        .setTabListener(tabListener));

导致这种情况发生的部分,任何人都可以帮我解决它吗?

【问题讨论】:

标签: android android-fragments tabs navigation


【解决方案1】:

您必须在添加标签之前调用actionBar.removeAllTabs()...因为您没有调用actionBar.removeAllTabs() 他们累积了 (4+4)...

附:请不要再使用操作栏和导航模式了..Action bar navigation modes are deprecated in Android L

将 actionBar 替换为工具栏,将选项卡替换为 PagerTabStrip 或其他...

【讨论】:

【解决方案2】:

onCreateView() 方法会在 frament 附加到 Activity 并且必须创建其视图时调用,然后将多次调用将选项卡添加到 ActionBar 的代码,这会导致您的问题。 由于 ActionBar 属于您的 Activity,而不是您的 Fragment,因此您最好在 Activity 中为其添加选项卡,将以下代码移至您的 Activity 应该会有所帮助:

private ActionBar actionBar;
private String[] tabs={ "1", "2", "3","4" };

actionBar=getActivity().getActionBar(); // remove getActivity().
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            // show the given tab
            // This line need to be changed, you can make a public method in 
            // your fragment which set the current item of ViewPager, and  
            // call the method in the Activity.
            mViewPager.setCurrentItem(tab.getPosition());
        }

        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // hide the given tab
        }

        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // probably ignore this event
        }
    };
    for (String tab_name : tabs) {
         actionBar.addTab(actionBar.newTab().setText(tab_name)
                        .setTabListener(tabListener));
    }

【讨论】:

    猜你喜欢
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    相关资源
    最近更新 更多