我想用一个新的片段来替换一个旧片段,但是我仍然可以得到在新片段中仍然可见的旧片段的按钮.
FragmentTransaction transaction = getFragmentManager().beginTransaction(); Fragment newFragment = GenericMood.newInstance("a","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.addToBackStack(null); transaction.commitAllowingStateLoss();
我可以用新的片段替换旧的片段,但是来自R.id.allmoods片段的按钮在新的片段上仍然可见.
我试着用下面给出的代码.
FragmentTransaction transaction = getFragmentManager().beginTransaction(); Fragment newFragment = GenericMood.newInstance("a",// and add the transaction to the back stack if needed transaction.replace(((ViewGroup)getView().getParent()).getId(),newFragment); transaction.addToBackStack(null); transaction.commitAllowingStateLoss();
XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/allmoods" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary" tools:context="com.moodoff.Moods"> <Button android:text="Button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="64dp" android:id="@+id/btn_btn" android:height="80dp" android:width="100dp" android:onClick="putmeoff" android:layout_marginLeft="17dp" android:layout_marginStart="17dp"/> </RelativeLayout>
这是应该取代以上的片段:
<FrameLayout 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:layout_width="match_parent" android:id="@+id/genericmood" android:layout_height="match_parent" android:background="@color/colorPrimary" tools:context="com.moodoff.GenericMood"> <!-- TODO: Update blank fragment layout --> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000000" android:layout_gravity="fill_horizontal" android:id="@+id/floatingButtons" > <android.support.design.widget.FloatingActionButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="1dp" android:layout_marginRight="14dp" app:backgroundTint="#ffffff" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:src="@drawable/cameraicon" android:id="@+id/btn_camera" app:fabSize="mini" /> </RelativeLayout> </FrameLayout>
两者都不行.该怎么办?
更新:使用适当的容器替换按钮后,新的片段未正确实例化.我得到一个纯白色的白色屏幕.
my activity_alltabs.xml looks like this: <?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.moodoff.AllTabs"> <android.support.design.widget.AppBarLayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/background_dark" /> </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.CoordinatorLayout>
解决方法
要了解片段转换的流程,首先,您必须了解其活动结构.
让我们来看看:
a)活动:在一切的底部(MainActivity)
让我们来看看:
a)活动:在一切的底部(MainActivity)
activity_main.xml: –
<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"> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
这里@ id / container是我们做的片段内容的转换的布局.
B)片段A:最初添加片段到MainActivity容器.
FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); //Instance of fragment Fragment newFragment = FragmentA.newInstance("a","b"); //It will replace the fragment content view to container of main activity ft.replace(R.id.container,newFragment); //FragmentA is added to back stack with it's name as a tag ft.addToBackStack(FragmentA.class.getSimpleName()); ft.commitAllowingStateLoss();
B)片段B:用FragmentB替换片段A.
FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); //Instance of fragment Fragment newFragment = FragmentB.newInstance("a","b"); //It will replace the fragment content view to container of fragment A which // is prevIoUsly replaced to main activity container ft.replace(R.id.container,newFragment); //FragmentB is added to back stack with it's name as a tag ft.addToBackStack(FragmentB.class.getSimpleName()); ft.commitAllowingStateLoss();