【问题标题】:Android: FragmentTabHost: Navigating Fragment Overlapping Issues with tabsAndroid:FragmentTabHost:使用标签导航片段重叠问题
【发布时间】:2014-03-20 09:30:17
【问题描述】:

我有一些示例 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);

Fragment3 类

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) 然后我按tab,Fragment4的布局是重叠的 在所有屏幕中

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

**

请为我提供最好的方法。

【问题讨论】:

  • 您找到解决方案了吗?我也面临类似的问题
  • 它的替代品是什么??
  • 我刚刚使用了活动。每个活动屏幕都有我在此布局中提到的选项卡图标
  • 您在每个屏幕上创建标签布局?

标签: android android-fragments fragment


【解决方案1】:

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

这是正在发生的事情:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    相关资源
    最近更新 更多