Android 碎片在活动中出现

Android 碎片在活动中出现,android,Android,我想在单击listview上的项目时显示一个片段。listview位于主活动中。问题是片段出现在活动列表视图上。你知道问题是什么吗 主要活动: public class MainActivity extends AppCompatActivity { ArrayList<String> test = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstance

我想在单击listview上的项目时显示一个片段。listview位于主活动中。问题是片段出现在活动列表视图上。你知道问题是什么吗

主要活动:

public class MainActivity extends AppCompatActivity {

    ArrayList<String> test = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView) findViewById(R.id.listView);

        test.add("item 1");
        test.add("item 2");

        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, test);
        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                EditorFragment editorFragment = new EditorFragment();

                getSupportFragmentManager().beginTransaction()
                        .add(R.id.fragment_container, editorFragment).commit();
            }
        });
    }
**activity main xml**

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />
</android.support.constraint.ConstraintLayout>
我在片段中使用了removeAllViews方法,但当单击项目时,该方法不起作用。片段不会出现

public class EditorFragment extends Fragment {

        private EditText editor=null;


        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View result=inflater.inflate(R.layout.fragment_editor, container, false);

            if(container != null)
            {
                container.removeAllViews();
            }

            editor=(EditText) (result.findViewById(R.id.editor));

            return result;

        }
    }

我不确定是否会出现这个问题,但如果不使用任何片段,您可以尝试将容器可见性设置为“不可见”,并在需要时显示它

那么,你想要什么,而不是炫耀自己的活动呢?是否要将其显示为对话框?还是怎样
public class EditorFragment extends Fragment {

    private EditText editor=null;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View result=inflater.inflate(R.layout.fragment_editor, container, false);

        editor=(EditText) (result.findViewById(R.id.editor));

        return result;

    }
}
public class EditorFragment extends Fragment {

        private EditText editor=null;


        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View result=inflater.inflate(R.layout.fragment_editor, container, false);

            if(container != null)
            {
                container.removeAllViews();
            }

            editor=(EditText) (result.findViewById(R.id.editor));

            return result;

        }
    }