繁体   English   中英

Android:简单地用另一个片段替换片段

[英]Android: simple replace fragment with an other fragment

我的代码遇到了问题。

我想要的是

我的活动从Fragment01自动开始。 在Fragment01上,有一个按钮,应将Fragment01替换为Fragment02

怎么了

当我按下按钮时,我仍然可以在后台看到Fragment01,而不是替换Fragment01

MainActivity.java:

package com.lucky.fragmentexample;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    Button bn;
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bn = (Button)findViewById(R.id.button1);

        bn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            Fragment02 fragment02 = new Fragment02();
            fragmentTransaction.replace(R.id.button_fragment_container, fragment02);
            fragmentTransaction.commit();
            }
        });
    }
}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@+id/button_fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <fragment
            android:id="@+id/fragment01"
            android:name="com.lucky.fragmentexample.Fragment01"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>
</LinearLayout>

布局XML中的fragment元素只能引用单个片段,而不能按照您希望的方式换出。 相反,您需要完全从布局XML中删除fragment元素,并通过活动的onCreate()方法中的代码将Fragment01添加到容器FrameLayout中。

因此,删除fragment元素,就像这样;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <FrameLayout
       android:id="@+id/button_fragment_container"
       android:layout_width="match_parent"
       android:layout_height="match_parent" 
       />
</LinearLayout>

然后,您可以在onCreate()覆盖的开头执行事务,就像这样;

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.button_fragment_container, new Fragment01());
    transaction.commit();

您将需要为每个新事务实例化一个新的FragmentTransaction ,这意味着您可能希望从实例成员中删除FragmentManagerFragmentTransaction ,并在每次需要进行片段事务时将它们声明为新鲜。

我希望这有帮助。

进一步阅读:

将beginTransaction放在onclick函数中,并将R.id.button_fragment_container替换为R.id.frame_container。

在onclick中使用此命令:

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, new Fragment02()).commit();

xml文件中的片段不正确。 擦除该部分。

试试这个代码:

     FragmentManager fm = getFragmentManager();
     FragmentTransaction fragmentTransaction = fm.beginTransaction();
     fragmentTransaction.replace(R.id.fragment01, new Fragment02());
     fragmentTransaction.commit();

此外,在XML中将“片段”更改为“ FrameLayout”。 这样可以防止任何片段始终残留在后台。

只是一步一步走:

首先修改您的布局,删除那里的片段,然后在活动中引用FrameLayout。

现在启动要添加到framelayout的片段类,并调用以下代码:

FragmentTransaction transaction = manager.beginTransaction();
transaction.remove(removePreviousFragmentIfAny);
transaction.add(R.id.button_fragment_container, newFragment);
transaction.commit();

希望它能帮助您度过难关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM