FragmentTransaction的replace不能替换FrameLayout中的fragment

Android 码拜 8年前 (2016-09-16) 2261次浏览
以下是MainActivity的布局文件

<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=".MainActivity" >
    <FrameLayout 
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bt_three"
        >
	    <fragment
	    android:id="@+id/fragment_one"
	    android:name="tanare.cn.fragment.FragmentOne"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    />
	</FrameLayout> 
	<Button 
	    android:id="@+id/bt_two"
	    android:layout_width="match_parent"
	    android:layout_height="40dp"
	    android:text="two"
	    android:layout_alignParentBottom="true"
	    android:onClick="doclick"
	    />
</RelativeLayout>

fragmentOne的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
	<TextView 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:gravity="center_horizontal"
	    android:text="本人是"
	    android:textSize="20sp"
	    />
</RelativeLayout>

fragmentTwo的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
	<TextView 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:gravity="center_horizontal"
	    android:text="本人是fragmentTwo"
	    android:textSize="20sp"
	    />
</RelativeLayout>

在MainActivity中添加以下代码

public class MainActivity extends FragmentActivity {
	private FragmentTwo fTwo;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
        public void doclick(View view) {
		// TODO Auto-generated method stub
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		switch (view.getId()) {
		case R.id.bt_two:
			if(fTwo==null){
			fTwo = new FragmentTwo();
			}
			transaction.replace(R.id.fragment_one, fTwo);
			transaction.commit();
			break;
		}
        }
}

运行后出现重叠现象
FragmentTransaction的replace不能替换FrameLayout中的fragment

解决方案

10

把R.id.fragment_one改成fl_content。不过一般都是用add,添加进第二个再hide第一个。

10

引用:

把R.id.fragment_one改成fl_content。不过一般都是用add,添加进第二个再hide第一个。

+1
replace简单,但是你遇到这个问题是这种方法的常见问题,不推荐replace,推荐用add hide方法

15

引用:
Quote: 引用:

把R.id.fragment_one改成fl_content。不过一般都是用add,添加进第二个再hide第一个。

改成fl_content了还是替换不了,还是重叠在一起

<FrameLayout
android:id=”@+id/fl_content”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_above=”@+id/bt_three”
>
<fragment
android:id=”@+id/fragment_one”
android:name=”tanare.cn.fragment.FragmentOne”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
/>
</FrameLayout>
原因是你这样布局相当于把fragmentone,add到fl_content这样的话,这个framelayout就有两层了,replace就不能替换fragmentone了,你可以将fragmentone也用replace方式添加进去。或直接用,add和hide组合吧。

5

引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:

把R.id.fragment_one改成fl_content。不过一般都是用add,添加进第二个再hide第一个。

改成fl_content了还是替换不了,还是重叠在一起

<FrameLayout
android:id=”@+id/fl_content”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_above=”@+id/bt_three”
>
<fragment
android:id=”@+id/fragment_one”
android:name=”tanare.cn.fragment.FragmentOne”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
/>
</FrameLayout>
原因是你这样布局相当于把fragmentone,add到fl_content这样的话,这个framelayout就有两层了,replace就不能替换fragmentone了,你可以将fragmentone也用replace方式添加进去。或直接用,add和hide组合吧。

假如将fragment_one用replace来add进去,那不一样是add了吗?不是同样是两层了?有点追根究底了。

replace是替换不是添加。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明FragmentTransaction的replace不能替换FrameLayout中的fragment
喜欢 (0)
[1034331897@qq.com]
分享 (0)