Android 我在同一活动下的两个片段一个在另一个上面显示

Android 我在同一活动下的两个片段一个在另一个上面显示,android,android-layout,android-fragments,android-fragmentactivity,Android,Android Layout,Android Fragments,Android Fragmentactivity,我有一项活动: public class MainActivity_with_Fragment extends RoboFragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activity2

我有一项活动:

public class MainActivity_with_Fragment extends RoboFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity2);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new SettingsFragment())
                    .add(R.id.container, new AddReminderFragment())
                    //.add(R.id.container, new ReadRemindersFragment())
                    .commit();
        }
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
使用此布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/container"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context="com.example.stopcall.app.activities.dele_MainActivity2_with_Fragment"
             tools:ignore="MergeRootFrame"/>

我希望它分别承载3个片段(每次仅显示一个片段):

项添加到DB片段

从数据库中读取

凝固碎片


您建议如何设计和实现此功能?正确的方法是什么?

将两个片段添加到同一个容器中。如果一次只需要显示一个,请执行此操作

getSupportFragmentManager().beginTransaction()
   .add(R.id.container, new SettingsFragment()).commit();

getSupportFragmentManager().beginTransaction(
   .replace(R.id.container, new AddReminderFragment()).commit();
但是如果您需要同时显示这两个容器,那么创建一个新容器(比如
R.id.container\u two
)并执行此操作

getSupportFragmentManager().beginTransaction()
     .add(R.id.container, new SettingsFragment()).commit();
getSupportFragmentManager().beginTransaction()
    .add(R.id.container_two, new AddReminderFragment()).commit();

您正在同一容器中添加这两个片段。如果您一次只需要显示一个,则用户
替换
方法不
添加
。但如果您需要同时显示两者,则将它们添加到不同的容器中。请看我的回答,我将如何从一个片段移动到第二个片段?用一个容器?啊,我看到了替换。还有其他设置所有片段并在它们之间移动的动态方法吗?这取决于您试图做什么。试试这段代码,看看你的问题是否解决了。然后你需要使用片段寻呼机。如果需要将片段向左/向右滑动,请参见此图,然后这不是执行此操作的方法(请参见我之前的注释)。我的代码为您提供了最初要求的解决方案。