FragmentTransaction中replace方法不起作用

anshshan 2015-10-14 10:25:04
各位大神求指教,本人初学Android,遇到一个问题,使用FragmentTransaction中replace方法的时候不能移除之前的fragment,新fragment只是覆盖在之前的fragment之上,主要代码如下:
MainActivity中的代码:
public class MainActivity extends FragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AnotherRightFragment fragment = new AnotherRightFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
}

activity_main.xml中的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment
android:id="@+id/left_fragment"
android:name="edu.lzjtu.fragment.LeftFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />

<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >

<fragment
android:id="@+id/right_fragment"
android:name="edu.lzjtu.fragment.RightFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>

</LinearLayout>

...全文
2158 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hyominli 2017-07-21
  • 打赏
  • 举报
回复
引用 8 楼 hanshou101 的回复:
[quote=引用 5 楼 luosiye312 的回复:] 布局里不用放fragment。直接放一个FrameLayout。 然后调用以下方法:
getSupportFragmentManager().beginTransaction().replace(R.id.home_container, targetFragment, "fragment")
				.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commitAllowingStateLoss();
非常感谢!!!我的3个Fragment里怎么切换都不显示东西,原因就在于我写了<fragment> 照您说的写一个FrameLayout就一切都正常了![/quote] 成功了
  • 打赏
  • 举报
回复
引用 5 楼 luosiye312 的回复:
布局里不用放fragment。直接放一个FrameLayout。 然后调用以下方法:
getSupportFragmentManager().beginTransaction().replace(R.id.home_container, targetFragment, "fragment")
				.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commitAllowingStateLoss();
非常感谢!!!我的3个Fragment里怎么切换都不显示东西,原因就在于我写了<fragment> 照您说的写一个FrameLayout就一切都正常了!
LLNG 2016-04-21
  • 打赏
  • 举报
回复
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_1:
                AnotherRightFragment anotherRightFragment=new AnotherRightFragment();
                FragmentManager fragmentManager=getFragmentManager();
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment_right,anotherRightFragment);
//              fragmentTransaction.add(R.id.fragment_right,anotherRightFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                break;
            default:
                break;
        }
    }
LLNG 2016-04-21
  • 打赏
  • 举报
回复
我也遇到此种问题。。。《第一行代码》中的内容,只有add()方法好使,replace()和addToBackStack()都不好使,不知道问题出在哪里?楼主,你现在知道问题出在哪里了吗?
luosiye312 2015-10-15
  • 打赏
  • 举报
回复
布局里不用放fragment。直接放一个FrameLayout。 然后调用以下方法:
getSupportFragmentManager().beginTransaction().replace(R.id.home_container, targetFragment, "fragment")
				.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commitAllowingStateLoss();
anshshan 2015-10-15
  • 打赏
  • 举报
回复
引用 2 楼 sjj370216540 的回复:
你在xml 上 默认显示一个fragment,这样写,就是不能替换掉的 在代码上写,让它显示默认的(add()或replace()),这样的才能替换
谢谢大神,刚才试了一下,解决了!能实现基本的功能。多谢
z523714705 2015-10-15
  • 打赏
  • 举报
回复
其实你可以不用这样写,xml文件里面就写一个布局,来回切换的fragment 用FragmentTransaction add(),replace()来操作
sjj370216540 2015-10-15
  • 打赏
  • 举报
回复
你在xml 上 默认显示一个fragment,这样写,就是不能替换掉的 在代码上写,让它显示默认的(add()或replace()),这样的才能替换
anshshan 2015-10-15
  • 打赏
  • 举报
回复
大神们,求指教啊。。。。

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧