tabs - FragmentTabHost 显示重叠的片段

标签 tabs android-fragments android-tabhost overlap

我在一个 FragmentActivity 中使用一个 FragmentTabHost,它有 3 个选项卡。 如果我从每个选项卡的第一页更改选项卡,它工作正常,但如果我导航在一个选项卡内抛出片段,然后更改选项卡,它会显示与旧片段重叠的新片段。 有人可以帮助我吗?

这是FragmentActivity的布局(fragments_tab.xml):

    <?xml version="1.0" encoding="UTF-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/background_gradient">

    <ImageView
        android:contentDescription="@string/app_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:src="@drawable/background_gradient" />

    <android.support.v4.app.FragmentTabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>

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

    </LinearLayout>
    </android.support.v4.app.FragmentTabHost>
    </RelativeLayout>

我的 FragmentActivity 的代码:

    public class MainActivity extends FragmentActivity {

        private FragmentTabHost mTabHost;

        @Override   
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setDB();
            setContentView(R.layout.fragments_tab);

            mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);

            mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);


            mTabHost.addTab(mTabHost.newTabSpec("Home").setIndicator("Home",      getResources().getDrawable(R.drawable.ic_menu_home)),
                    HomeFragment.class, null);

            mTabHost.addTab(mTabHost.newTabSpec("Map").setIndicator("Map", getResources().getDrawable(R.drawable.ic_menu_mapmode)),
                    MapMenuFragment.class, null);
            mTabHost.addTab(mTabHost.newTabSpec("Info").setIndicator("Info", getResources().getDrawable(R.drawable.ic_menu_info_details)),
                    InfoFragment.class, null);
        }
}

每个片段都是这样创建的:

public class InfoFragment extends Fragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_info, container, false);
        return view;
    }

有人可以帮助我吗?

最佳答案

我找到了问题的解决方案。 在我的项目中,每个片段都是扩展类 Fragment 的不同类。当 FragmentTabHost 尝试分离旧片段时,它找不到 Fragment 对象,因此它没有删除任何内容。

我不是那么专家,我确​​信有一种更简单的方法,但经过多次头痛后,我发现这个解决方案有效。 这就是我所做的:

  • 我将每个选项卡的当前片段保存到事件中的对象变量中。
  • 我将 FragmentTabHost 的代码复制到自己的类中,并在调用分离或附加时更改了 doTabChanged 方法。

(我有 3 个选项卡“主页”、“ map ”和“信息”)

分离:

//ft.detach(mLastTab.fragment);
if(mLastTab.tag == "Home") ft.detach((Fragment) tabHomeFragment);
else if(mLastTab.tag == "Info") ft.detach((Fragment) tabInfoFragment);
else ft.detach((Fragment) tabMapFragment);

附上:

//ft.attach(newTab.fragment);
if(newTab.tag == "Home") ft.attach((Fragment) tabHomeFragment);
else if(newTab.tag == "Info") ft.attach((Fragment) tabInfoFragment);
else ft.attach((Fragment) tabMapFragment);

tabHomeFragment、tabMapFragment 和 tabInfoFragment 包含每个选项卡的当前片段

关于tabs - FragmentTabHost 显示重叠的片段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14876358/

相关文章:

jquery - 用户单击导航选项卡时如何设置事件类

apache-flex - flex 4 标签栏 - 禁用标签

c++ - 如何在运行时从 Glade 定义中添加 Gtk::Notebook 选项卡?

ios - 如何使用 UITabBarController (Swift) 在选项卡之间发送 NSNotifications

android - 使用 MVVMCross 在 Android 中进行 fragment 转换

android-fragments - Android - Recyclerview 共享元素过渡项位置

java - 两个 Fragment 永远不应该直接通信

android - 如何在选项卡 Activity 中创建子 Activity

android其他activity点击返回键到listview,listview项无法在选项卡下点击

android - TabHost的Activity可以是ListAcitivty吗?