安卓:FragmentTabHost:与标签导航片段重叠问题 [英] Android: FragmentTabHost: Navigating Fragment Overlapping Issues with tabs

查看:390
本文介绍了安卓:FragmentTabHost:与标签导航片段重叠问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了一些示例FragmentTabHost项目。我做了一些修改,这需要为我的项目。 我的标签栏XML(bottom_tabs.xml)

I have got some sample FragmentTabHost project. And I made some modifications which are required for my project. my tab bar xml (bottom_tabs.xml)

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

oncreate() - FragmentActivity

mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

        Bundle b = new Bundle();
        b.putString("key", "Simple");
        mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
                Fragment1.class, b);
        //
        b = new Bundle();
        b.putString("key", "Contacts");
        mTabHost.addTab(mTabHost.newTabSpec("contacts")
                .setIndicator("Contacts"), Fragment2.class, b);
        b = new Bundle();
        b.putString("key", "Custom");
        mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
                Fragment3.class, b);

Fragment3类

Fragment3 class

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

        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_3,
                null);  
        Button b = (Button) root.findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                 Fragment f;
                    f = (Fragment) new Fragment4();
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.replace(R.id.realtabcontent, f);
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    ft.addToBackStack(null);
                   ft.commit();
            }
        });
        return root;
    }

中存在的问题:        **

The Problems: **

  • 1)如果我美元的Fragment3 p $ PSS goto1按钮,它重定向到Fragment4。 没有问题

  • 1) If I press goto1 button in Fragment3 , It redirects to Fragment4. No Issues

2)然后我preSS的选项卡,在Fragment4的布局重叠 在所有屏幕

2) Then I press the tabs, the layout of the Fragment4 is overlapped in all the screens

3)如果我preSS转到第二个选项卡按钮,如何重定向到Fragment2和 同时第二个选项卡应加以强调。

3) If I press goto 2nd tab button, How to redirect to Fragment2 and same time 2nd tab should be highlighted.

**

请为我做到这一点的最好办法。

Please provide me the best way to do this.

推荐答案

要更改选项卡您应该使用,而不是使用<$ C $的 FragmentTabHost.setCurrentTab()方法C> FragmentTransaction 自己的(它是内部处理)的。您Fragment4正出现重叠的原因是因为它是当你改变标签永远不会被删除。

To change tabs your should use the FragmentTabHost.setCurrentTab() method instead of using FragmentTransaction yourself (it is handled internally). The reason your Fragment4 is appearing overlapped is because it is never removed when you change tabs.

这是正在发生的事情:

  1. 您导航到选项卡中。
  2. 您$​​ P $ PSS的GOTO fragment4按钮,这会导致你的code与fragment4取代fragment3
  3. 您$​​ P $ PSS另一个选项卡的指标。这将导致FragmentTabHost删除当前片段的(它仍然认为是Fragment3)的并添加新的片段。该Fragment4,您手动添加,还是有的。为了避免这种情况,你可以添加一个侦听器选项卡更改事件,并明确删除Fragment4如果有..甚至更好,你可以添加Fragment4作为Fragment3的子片段。当你离开TAB3它会消失,当你回到TAB3将再次出现这样的。这只是取决于你正在努力完成的任务。
  1. You navigate to tab 3.
  2. You press the goto fragment4 button which causes your code to replace fragment3 with fragment4
  3. You press another tab indicator. This causes the FragmentTabHost to remove the current fragment (which it still thinks is Fragment3) and add the new Fragment. The Fragment4, which you added manually, is still there. To avoid this you could add a listener for tab change events and explicitly remove Fragment4 if it is there.. Or even better you could add Fragment4 as a child fragment of Fragment3. That way when you leave Tab3 it will go away, and it will appear again when you return to Tab3. It just depends on what you are trying to accomplish.

这篇关于安卓:FragmentTabHost:与标签导航片段重叠问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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