5

我知道发布了SO与该问题相关的各种问题,但没有一个为我提供解决方案。我有 4 个标签fragment activity,每个标签都有自己的fragment. 我要进入另一个fragment里面。当我切换到选项卡时,有时它会重叠并且两者都是可见的。

这是代码(xml):

<Button
    android:id="@+id/checkBal"
    android:layout_width="fill_parent"
    android:layout_height="45dp"
    android:clickable="false"
    android:gravity="left|center_vertical"
    android:typeface="serif" />

<View
    android:layout_width="fill_parent"
    android:layout_height="2dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="2dp"
    android:background="@color/green" />

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

public class Tab_Activity extends FragmentActivity {

FragmentTabHost mHost;
Button balance;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs);

    balance = (Button) findViewById(R.id.checkBal);
    mHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    mHost.addTab(mHost.newTabSpec("Home").setIndicator("Home"),
            Home_Fragment.class, null);
    mHost.addTab(mHost.newTabSpec("Number").setIndicator("Send to number"),
            Number_Fragment.class, null);
    mHost.addTab(
            mHost.newTabSpec("Contact").setIndicator("Send to contacts"),
            Contact_Fragment.class, null);
    mHost.addTab(mHost.newTabSpec("Group").setIndicator("Send to groups"),
            Group_Fragment.class, null);

    mHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub

            if (tabId.equalsIgnoreCase("Home")) {

                Home_Fragment home = new Home_Fragment();

                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();
                ft.replace(R.id.realtabcontent, home, "Home");
                ft.commit();

            }

            if (tabId.equalsIgnoreCase("Number")) {

                Number_Fragment number = new Number_Fragment();

                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();
                ft.replace(R.id.realtabcontent, number, "Number");
                ft.commit();

            }
            if (tabId.equalsIgnoreCase("Contact")) {

                Contact_Fragment contact = new Contact_Fragment();

                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();
                ft.replace(R.id.realtabcontent, contact, "Contact");
                ft.commit();
            }

            if (tabId.equalsIgnoreCase("Group")) {

                Group_Fragment group = new Group_Fragment();

                FragmentManager manager = getSupportFragmentManager();
                FragmentTransaction ft = manager.beginTransaction();

                ft.replace(R.id.realtabcontent, group, "Group");
                ft.commit();
            }

        }
    });

}




public void newFragment(Fragment fragment, String tag) {

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    //
    if (tag.equalsIgnoreCase("Group"))
        ft.remove(new Group_Fragment());
    else
        ft.remove(new Contact_Fragment());
    ft.replace(R.id.realtabcontent, fragment);
    ft.addToBackStack(null);
    ft.commit();

}

public void updateContacts(ArrayList<String> cList) {

    FragmentManager manager = getSupportFragmentManager();

    Contact_Fragment contactFrag = (Contact_Fragment) manager
            .findFragmentByTag("Contact");
    contactFrag.updateData(cList);

}

 }

在新片段中调用片段

mActivity.newFragment(new Select_Group_Fragment(), "Group");

当我从另一个片段中调用片段时,我需要addToBackStacknew fragment方法中添加Tab_Activity

重叠的屏幕截图

请帮助伙计们

4

3 回答 3

0

在 AndroidMainifest.xml 更改中:

android:name=".activities.youactivityname"
android:configChanges="screenSize|orientation"
于 2015-02-03T21:01:49.107 回答
0

从我在 Fragments 上看到的所有内容,即 Google IO、StackOverflow 等,这不是处理它的方法。活动应该控制哪些片段是活动的。片段应该服从片段控制的活动。您可能必须将片段之间的数据传递给活动。谷歌开发者强烈反对片段到片段的通信。片段是屏幕的一个子集。

您的叠加看起来像您同时有 2 个片段处于活动状态。

供参考。最好使用 Fragment Transactions 在一个 Activity 中操作多个 Fragment。从 xml 启动一个片段的活动不能真正被片段事务操作。

希望这可以帮助。

于 2013-07-04T05:42:07.657 回答
0
layout.removeAllViews();

use this to remove previous view

于 2015-05-27T16:38:23.577 回答