gpt4 book ai didi

tabs - FragmentTabHost 显示片段重叠

转载 作者:行者123 更新时间:2023-12-04 05:11:26 26 4
gpt4 key购买 nike

我在一个 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 对象,因此它没有删除任何内容。

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

  • 我将每个选项卡的当前片段保存到事件中的 Object 变量中。
  • 我将 FragmentTabHost 的代码复制到自己的类中,并在调用 detach 或 attach 时更改了 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/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com