2

我有一些示例 FragmentTabHost 项目。我做了一些我的项目所需的修改。我的标签栏 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);

在此处输入图像描述

片段3类

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;
    }

在此处输入图像描述 在此处输入图像描述

问题:**

  • 1)如果我在 Fragment3 中按下 goto1 按钮,它会重定向到 Fragment4。没有问题

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

  • 3)如果我按下转到第二个选项卡按钮,如何重定向到 Fragment2 并同时突出显示第二个选项卡。

**

请给我最好的方法来做到这一点。

4

1 回答 1

0

要更改选项卡,您应该使用该FragmentTabHost.setCurrentTab()方法而不是使用您FragmentTransaction自己(它在内部处理)。您的 Fragment4 出现重叠的原因是因为在您更改选项卡时它永远不会被删除。

这是正在发生的事情:

  1. 您导航到选项卡 3。
  2. 您按下 goto fragment4 按钮,这会导致您的代码将 fragment3 替换为 fragment4
  3. 您按下另一个标签指示器。这会导致 FragmentTabHost 删除当前片段(它仍然认为是 Fragment3)并添加新的 Fragment。您手动添加的 Fragment4 仍然存在。为避免这种情况,您可以为选项卡更改事件添加一个侦听器,并显式删除 Fragment4(如果存在)。或者更好的是,您可以将 Fragment4 添加为 Fragment3 的子片段。这样当你离开 Tab3 时它会消失,当你返回 Tab3 时它会再次出现。这仅取决于您要完成的工作。
于 2014-03-20T01:30:17.673 回答