片段未在导航抽屉活动中被替换

时间:2019-02-06 07:44:46

标签: android navigation-drawer

我正在尝试用片段的内容替换content_main.xml的内容。但是,它们并没有被替换,而只是添加在其顶部。

MainActivity.java

private void displaySelectedScreen(int id){
    //Fragment fragment = null;

    switch (id){
        case R.id.nav_menu1: //fragment = new Menu1();
            getSupportFragmentManager().beginTransaction().replace(R.id.content_main, new Menu1()).commit();
            break;
        case R.id.nav_menu2: //fragment = new Menu2();
            getSupportFragmentManager().beginTransaction().replace(R.id.content_main, new Menu2()).commit();
            break;
        case R.id.nav_menu3: //fragment = new Menu3();
            getSupportFragmentManager().beginTransaction().replace(R.id.content_main, new Menu3()).commit();
            break;
    }

    /*if(fragment != null){
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.content_main, fragment).commit();
    }*/

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    displaySelectedScreen(id);
    return true;
}

Menu1.java

    import android.os.Bundle;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class Menu1 extends Fragment {
        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, 
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
        {
            return inflater.inflate(R.layout.menu1, container, false);
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            getActivity().setTitle("Title 1");
        }
    }

content_main.xml

<LinearLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main"
    android:id="@+id/content_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Content main"/>

</LinearLayout>

menu1.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="menu1"/>
</LinearLayout>

P.S:我已经阅读了关于StackOverflow的几乎所有答案,但是我的问题没有得到解决。

0 个答案:

没有答案