我有一个标签式活动,有三个标签(比如A,B,C)。我为每一个都有片段,我在每个标签中显示。 主要活动XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.tabbedandfragment.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
此片段的代码:
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_a, container, false);
Button replaceWithNewFragemnt = (Button)view.findViewById(R.id.replaceonclick);
replaceWithNewFragemnt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Fragment newFragment = ReplaceA.newInstance("replacedA","b");
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
//transaction.replace(R.id.allmoods, newFragment);
transaction.replace(R.id.main_content, newFragment);
transaction.addToBackStack("mainA");
transaction.commitAllowingStateLoss();
}
});
return view;
}
XML for this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tabbedandfragment.A"
android:id="@+id/mainA"
>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="I am main A"
android:textStyle="normal|bold"
android:textSize="30sp" />
<Button
android:text="Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="181dp" />
<Button
android:text="Click me to replace with new fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/replaceonclick"
android:layout_marginTop="55dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
点击按钮&#34;点击我以更换新片段&#34;后,新片段将替换旧片段,但控件仍保留旧片段。发生类似这样的事情:
请帮助我。
答案 0 :(得分:0)
你想要替换什么? 您使用ACTIVITY布局ID作为容器,尝试使用ViewPager id