user8487655
user8487655

Reputation:

fragment is appearing over activity

I want to show a fragment when a item on the listview is clicked. The listview is in the main activity. The issue is that the fragment is appearing over the activity listview. Do you know what is the issue?

main activity:

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>

fragment editor xml

<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/editor"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top|left"
    android:inputType="text"
    android:hint="@string/hint"/>

EditorFragment

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;

    }
}

I used the removeAllViews method in the fragment, but its not working when the item is clicked the fragment dont appears.

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;

        }
    }

Upvotes: 1

Views: 37

Answers (1)

Vrong
Vrong

Reputation: 26

I'm not sure to get the issue, but maybe you can try to set your container visibility to invisible when you don't use any fragment and show it when needed.

Upvotes: 1

Related Questions