我知道发布了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");
当我从另一个片段中调用片段时,我需要addToBackStack
在new fragment
方法中添加Tab_Activity
请帮助伙计们