如何在FragmentTabHost中制作特定的选项卡具有多个片段导航,例如tabgroupactivity [英] How to make particular tab in FragmentTabHost has multiple fragment navigations like tabgroupactivity

查看:105
本文介绍了如何在FragmentTabHost中制作特定的选项卡具有多个片段导航,例如tabgroupactivity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FragmentTabHost创建了三个选项卡.现在,我需要使所有导航都在选项卡下.我怎么能得到这个.我需要获得使用TabGroupActivity获得的东西.

I created three tabs using FragmentTabHost. Now I need to make all the navigation are under the tabs. How can I get this. I need to get what we get using TabGroupActivity.

Tab1 ---> frag1-> frag2

Tab1 --->frag1-->frag2

Tab2 ---> frag3

Tab2 --->frag3

Tab3 ---> frag4 ---> frag5

Tab3 --->frag4--->frag5

我使用过fragmentTransaction.add(),fragmentTransaction.remove(),fragmentTransaction.replace().这三种方法无济于事.

I have used fragmentTransaction.add(), fragmentTransaction.remove(), fragmentTransaction.replace(). These three methods but nothing give solution.

替换方法在现有片段视图之上显示新的片段视图.

Replace method shows new fragment view on top of existing fragment view.

删除添加,从这两个仅删除"有效,添加"无效.

Remove and Add, from these two remove only works, add does not works.

谢谢.

TabHostMain.java

TabHostMain.java

    @Override
    protected void onCreate(Bundle savedInstanceState)
 {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.bottom_tabs);

mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
                mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
            View view;
    view=getTabView(R.drawable.ic_launcher);

    Bundle b = new Bundle();
    b.putString("key", "Simple");
    mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator(view),Fragment1.class, null);

    b = new Bundle();
    b.putString("key", "Contacts");
    view=getTabView(R.drawable.ic_launcher);
    mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator(view), Fragment2.class, null);

    b = new Bundle();
    b.putString("key", "Custom");
    view=getTabView(R.drawable.ic_launcher);
    mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator(view),Fragment3.class, null);

}

Fragment3.java

Fragment3.java

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        View view=LayoutInflater.from(getActivity()).inflate(R.layout.activity_second, null);

        ((TextView)view.findViewById(R.id.second_act_text)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentManager fm=getChildFragmentManager();
                FragmentTransaction fragmentTransaction=fm.beginTransaction();
                fragmentTransaction.replace(R.id.second_activity, new Fragment1()).addToBackStack(null).commit();
            }
        });
        return view;
    }

推荐答案

每个选项卡中不同片段导航堆栈的概念已经在stackoverflow上讨论了很多次,例如:

The concept of different fragment navigation stacks in each tab has been discussed quite a few times on stackoverflow, one example for instance:

在Android中使用每个标签为单独的Back Stack碎片

一种无需管理您自己的自定义导航后退堆栈的简单/粗暴方法是在每个选项卡下都有一个根片段,然后每当一个根片段想要导航到另一个片段(片段B)时,只需显示一个最初带有片段B的新活动,并且Activity将有其自己的片段导航回堆栈.

One simple/crude way of achieving this without having to manage your own custom navigation back stack is to have a root fragment under each tab, and then whenever a root fragment wants to navigate to another fragment (fragment B) simply show a new Activity initially with fragment B and that Activity will have its own fragment navigation back stack.

Tab1 --->根frag1->活动(自己的导航反向堆栈)-> frag2

Tab1 --->root frag1 --> Activity (own nav back stack) --> frag2

Tab2 ---> root frag3

Tab2 --->root frag3

Tab3 --->根frag4->活动(自己的导航背堆栈)-> frag5-> frag6-> frag7

Tab3 --->root frag4 --> Activity (own nav back stack) --> frag5 --> frag6 --> frag7

StackAnywhere 应用.它大量使用选项卡,但是当您在这些选项卡中导航时,通常会将导航移至新的活动.但是,使用这种方法的YMMV.

An example of an app that does something like this is actually the StackAnywhere app. It makes heavy use of tabs, but when you navigate within those tabs it generally moves the navigation to a new Activity. YMMV with this approach, however.

这篇关于如何在FragmentTabHost中制作特定的选项卡具有多个片段导航,例如tabgroupactivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆