Android 如何实现 FragmentManager 和 FragmentTransaction 以仅替换单个片段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20401074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:47:43  来源:igfitidea点击:

How can I implement FragmentManager and FragmentTransaction to replace just a single fragment?

androidfragmentfragmenttransactionfragmentmanager

提问by Carl

I have an activity with 3 fragments; a header; a body; a footer (same point as in HTML). The bodyfragment contains three buttons which each should replace the middle fragment (body; itself) with another one, but I can't figure out how to work FragmentManager and FragmentTransition in here. I can't seem to find any coherency in other peoples questions on here with regards to the way others implement their fragments. It seems everyone has their own methods, or just doesn't include the full code in their threads.

我有一个包含 3 个片段的活动;一个标题;身体; 页脚(与 HTML 中的相同点)。bodyfragment 包含三个按钮,每个按钮都应该用另一个替换中间的片段(身体;本身),但我不知道如何在这里工作 FragmentManager 和 FragmentTransition。关于其他人实施他们的片段的方式,我似乎无法在其他人的问题中找到任何连贯性。似乎每个人都有自己的方法,或者只是没有在他们的线程中包含完整的代码。

MainActivity.java

主活动.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.test_frag);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

}

TestFragment.java

测试片段.java

public class TestFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.test_frag, container, false);
}

}

}

BodyFragment.java

BodyFragment.java

public class BodyFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    return inflater.inflate(R.layout.body_frag, container, false);
}

}

}

Fragment in XML

XML 中的片段

<fragment
    android:id="@+id/bodyfragment"
    android:name="com.example.test.BodyFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    tools:layout="@layout/body_frag" />

BodyFragment layout in XML (button x3)

XML 格式的 BodyFragment 布局(按钮 x3)

    <Button
    android:id="@+id/bsettings"
    android:layout_width="130dp"
    android:layout_height="40dp"
    android:layout_alignBaseline="@+id/bgames"
    android:layout_alignBottom="@+id/bgames"
    android:layout_toRightOf="@+id/bgames"
    android:text="SETTINGS" />

回答by Xaver Kapeller

You use the FragmentManager like this:

您可以像这样使用 FragmentManager:

FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.bodyfragment, AnotherFragment.newInstance()); // newInstance() is a static factory method.
transaction.commit();

This code would replace the Fragment which sits in the View with the id R.id.bodyfragment with a new Instance of another Fragment.

此代码将使用另一个 Fragment 的新实例替换 ID 为 R.id.bodyfragment 的 View 中的 Fragment。

EDIT:

编辑:

To create a new instance of another Fragment a static factory method is supposed to be used. You would implement them in your Fragment like this:

要创建另一个 Fragment 的新实例,应该使用静态工厂方法。您可以像这样在您的 Fragment 中实现它们:

public class AnotherFragment extends Fragment {

    public static AnotherFragment newInstance() {
        AnotherFragment fragment = new AnotherFragment();
        ...
        // do some initial setup if needed, for example Listener etc
        ...
        return fragment;
    }
    ...
}