Java Android导航抽屉片段

Java Android导航抽屉片段,java,android,xml,android-fragments,navigation-drawer,Java,Android,Xml,Android Fragments,Navigation Drawer,我想创建一个音乐播放器应用程序,其中包括一个导航抽屉,该抽屉是在Android Studio中默认创建的。我只想在片段中显示内容。例如,媒体控件或歌曲列表需要在片段中。单击任何导航菜单项时,它必须将旧片段替换为新片段,操作栏保持完整。然而,我不知道如何前进。目前,内容_main.xml通过活动_main.xml打开。请帮忙!! activity_main.xml <?xml version="1.0" encoding="utf-8"?> <android.support.

我想创建一个音乐播放器应用程序,其中包括一个导航抽屉,该抽屉是在Android Studio中默认创建的。我只想在片段中显示内容。例如,媒体控件或歌曲列表需要在片段中。单击任何导航菜单项时,它必须将旧片段替换为新片段,操作栏保持完整。然而,我不知道如何前进。目前,内容_main.xml通过活动_main.xml打开。请帮忙!! activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <android.support.v4.widget.DrawerLayout
 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:id="@+id/drawer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true"
 tools:openDrawer="start">

    <!-- The ActionBar -->
 <include layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    />

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

    <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#17BAFF"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

    </android.support.v4.widget.DrawerLayout>
活动\u main\u drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<group android:checkableBehavior="single"
    >
    <item android:id="@+id/nav_albums"
        android:icon="@drawable/albums"
        android:title="Albums"

        />
    <item android:id="@+id/nav_artists"

        android:icon="@drawable/artists"
        android:title="Artists" />
    <item android:id="@+id/nav_playlists"
        android:icon="@drawable/playlists"
        android:title="Playlists" />
    <item android:id="@+id/nav_songs"
        android:icon="@drawable/songs"
        android:title="Songs" />
 </group>

 <item android:title="Liked the app ?">
    <menu>
        <item android:id="@+id/nav_share"                  android:icon="@android:drawable/ic_menu_share"
            android:title="Share with Friends" />

    </menu>
    </item>
    </menu>

我是这样做的,我使用了导航视图。根据id更改您的操作

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerContent(navigationView);

    }

    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {                            
                        switch (menuItem.getItemId()) {
                            case R.id.action_settings:
                                menuItem.setChecked(true);
                                drawerLayout.closeDrawers();
                                doFragmentTransaction();
                        }
                        return true;
                    }
                });
    }
这是我的工具栏布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:elevation="2dp"
        android:theme="@style/Base.ThemeOverlay.AppCompat.Dark" />


    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:background="@android:color/holo_red_dark"
        android:layout_height="match_parent" />

</LinearLayout>

这是我的main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <!-- The main content view -->

    <include layout="@layout/toolbar" />


    <!-- The navigation drawer -->

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_main" />


</android.support.v4.widget.DrawerLayout>

我是这样做的,我使用了导航视图。根据id更改您的操作

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerContent(navigationView);

    }

    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {                            
                        switch (menuItem.getItemId()) {
                            case R.id.action_settings:
                                menuItem.setChecked(true);
                                drawerLayout.closeDrawers();
                                doFragmentTransaction();
                        }
                        return true;
                    }
                });
    }
这是我的工具栏布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:elevation="2dp"
        android:theme="@style/Base.ThemeOverlay.AppCompat.Dark" />


    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:background="@android:color/holo_red_dark"
        android:layout_height="match_parent" />

</LinearLayout>

这是我的main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <!-- The main content view -->

    <include layout="@layout/toolbar" />


    <!-- The navigation drawer -->

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_main" />


</android.support.v4.widget.DrawerLayout>

我是这样做的,我使用了导航视图。根据id更改您的操作

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerContent(navigationView);

    }

    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {                            
                        switch (menuItem.getItemId()) {
                            case R.id.action_settings:
                                menuItem.setChecked(true);
                                drawerLayout.closeDrawers();
                                doFragmentTransaction();
                        }
                        return true;
                    }
                });
    }
这是我的工具栏布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:elevation="2dp"
        android:theme="@style/Base.ThemeOverlay.AppCompat.Dark" />


    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:background="@android:color/holo_red_dark"
        android:layout_height="match_parent" />

</LinearLayout>

这是我的main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <!-- The main content view -->

    <include layout="@layout/toolbar" />


    <!-- The navigation drawer -->

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_main" />


</android.support.v4.widget.DrawerLayout>

我是这样做的,我使用了导航视图。根据id更改您的操作

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        if (navigationView != null) {
            setupDrawerContent(navigationView);

    }

    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {                            
                        switch (menuItem.getItemId()) {
                            case R.id.action_settings:
                                menuItem.setChecked(true);
                                drawerLayout.closeDrawers();
                                doFragmentTransaction();
                        }
                        return true;
                    }
                });
    }
这是我的工具栏布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tool_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primary"
        android:elevation="2dp"
        android:theme="@style/Base.ThemeOverlay.AppCompat.Dark" />


    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:background="@android:color/holo_red_dark"
        android:layout_height="match_parent" />

</LinearLayout>

这是我的main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
    <!-- The main content view -->

    <include layout="@layout/toolbar" />


    <!-- The navigation drawer -->

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/menu_main" />


</android.support.v4.widget.DrawerLayout>

以下代码可能对您有所帮助:

@SuppressWarnings("StatementWithEmptyBody")    
@Override    
public boolean onNavigationItemSelected(MenuItem item) {    
    // Handle navigation view item clicks here.    
    int id = item.getItemId();    
    Fragment fragment = null;    
    if (id == R.id.nav_camara) {    
        // Handle the camera action    
        fragment = new ImportFragment();    

    } else if (id == R.id.nav_gallery) {    
        fragment = new GalleryFragment();    
    } else if (id == R.id.nav_slideshow) {    

    } else if (id == R.id.nav_manage) {    

    } else if (id == R.id.nav_share) {    

    } else if (id == R.id.nav_send) {    

    }    

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();    

    // Replace whatever is in the fragment_container view with this fragment,    
    // and add the transaction to the back stack so the user can navigate back    
    transaction.replace(R.id.fragment_container, fragment);    
    transaction.addToBackStack(null);    

    // Commit the transaction    
    transaction.commit();    

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);    
    drawer.closeDrawer(GravityCompat.START);    
    return true;    
}

以下代码可能对您有所帮助:

@SuppressWarnings("StatementWithEmptyBody")    
@Override    
public boolean onNavigationItemSelected(MenuItem item) {    
    // Handle navigation view item clicks here.    
    int id = item.getItemId();    
    Fragment fragment = null;    
    if (id == R.id.nav_camara) {    
        // Handle the camera action    
        fragment = new ImportFragment();    

    } else if (id == R.id.nav_gallery) {    
        fragment = new GalleryFragment();    
    } else if (id == R.id.nav_slideshow) {    

    } else if (id == R.id.nav_manage) {    

    } else if (id == R.id.nav_share) {    

    } else if (id == R.id.nav_send) {    

    }    

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();    

    // Replace whatever is in the fragment_container view with this fragment,    
    // and add the transaction to the back stack so the user can navigate back    
    transaction.replace(R.id.fragment_container, fragment);    
    transaction.addToBackStack(null);    

    // Commit the transaction    
    transaction.commit();    

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);    
    drawer.closeDrawer(GravityCompat.START);    
    return true;    
}

以下代码可能对您有所帮助:

@SuppressWarnings("StatementWithEmptyBody")    
@Override    
public boolean onNavigationItemSelected(MenuItem item) {    
    // Handle navigation view item clicks here.    
    int id = item.getItemId();    
    Fragment fragment = null;    
    if (id == R.id.nav_camara) {    
        // Handle the camera action    
        fragment = new ImportFragment();    

    } else if (id == R.id.nav_gallery) {    
        fragment = new GalleryFragment();    
    } else if (id == R.id.nav_slideshow) {    

    } else if (id == R.id.nav_manage) {    

    } else if (id == R.id.nav_share) {    

    } else if (id == R.id.nav_send) {    

    }    

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();    

    // Replace whatever is in the fragment_container view with this fragment,    
    // and add the transaction to the back stack so the user can navigate back    
    transaction.replace(R.id.fragment_container, fragment);    
    transaction.addToBackStack(null);    

    // Commit the transaction    
    transaction.commit();    

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);    
    drawer.closeDrawer(GravityCompat.START);    
    return true;    
}

以下代码可能对您有所帮助:

@SuppressWarnings("StatementWithEmptyBody")    
@Override    
public boolean onNavigationItemSelected(MenuItem item) {    
    // Handle navigation view item clicks here.    
    int id = item.getItemId();    
    Fragment fragment = null;    
    if (id == R.id.nav_camara) {    
        // Handle the camera action    
        fragment = new ImportFragment();    

    } else if (id == R.id.nav_gallery) {    
        fragment = new GalleryFragment();    
    } else if (id == R.id.nav_slideshow) {    

    } else if (id == R.id.nav_manage) {    

    } else if (id == R.id.nav_share) {    

    } else if (id == R.id.nav_send) {    

    }    

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();    

    // Replace whatever is in the fragment_container view with this fragment,    
    // and add the transaction to the back stack so the user can navigate back    
    transaction.replace(R.id.fragment_container, fragment);    
    transaction.addToBackStack(null);    

    // Commit the transaction    
    transaction.commit();    

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);    
    drawer.closeDrawer(GravityCompat.START);    
    return true;    
}



您是否已初始化menu.xml文件中的菜单项?是的,我已初始化。菜单工作正常。您已经创建了所有片段,如歌曲列表和媒体控制等。对吗?您是否在menu.xml文件中初始化了菜单项?是的,我已初始化。菜单工作正常。您已经创建了所有片段,如歌曲列表和媒体控制等。对吗?您是否在menu.xml文件中初始化了菜单项?是的,我已初始化。菜单工作正常。您已经创建了所有片段,如歌曲列表和媒体控制等。对吗?您是否在menu.xml文件中初始化了菜单项?是的,我已初始化。菜单工作正常。您已经创建了所有片段,如歌曲列表和媒体控制等。对吗?我可以用新片段内容(歌曲列表)替换旧片段(即媒体播放器)。但问题是歌曲列表片段上没有显示操作栏。因为app_bar_main.xml有,所以操作栏只显示内容_main。还有一个疑问。如何在启动应用程序时在片段中显示内容_main.xml?不要在onstart/oncreate of Activity中执行片段事务确定无需担心。。。此外,我可以用新片段内容(歌曲列表)替换旧片段(即媒体播放器),因为这可能对其他人有所帮助。但问题是歌曲列表片段上没有显示操作栏。因为app_bar_main.xml有,所以操作栏只显示内容_main。还有一个疑问。如何在启动应用程序时在片段中显示内容_main.xml?不要在onstart/oncreate of Activity中执行片段事务确定无需担心。。。此外,我可以用新片段内容(歌曲列表)替换旧片段(即媒体播放器),因为这可能对其他人有所帮助。但问题是歌曲列表片段上没有显示操作栏。因为app_bar_main.xml有,所以操作栏只显示内容_main。还有一个疑问。如何在启动应用程序时在片段中显示内容_main.xml?不要在onstart/oncreate of Activity中执行片段事务确定无需担心。。。此外,我可以用新片段内容(歌曲列表)替换旧片段(即媒体播放器),因为这可能对其他人有所帮助。但问题是歌曲列表片段上没有显示操作栏。因为app_bar_main.xml有,所以操作栏只显示内容_main。还有一个疑问。如何在启动应用程序时在片段中显示内容_main.xml?不要在onstart/oncreate of Activity中执行片段事务确定无需担心。。。此外,我可以用新片段内容(歌曲列表)替换旧片段(即媒体播放器),因为这可能对其他人有所帮助。但问题在于歌曲列表片段,操作栏没有显示。由于app_bar_main.xml已打开,因此操作栏仅针对内容_main显示。任何解决方案??您应该尝试用Yaa替换。但是如何在启动应用程序时在片段中显示内容?并将内容放入app_bar_main.xml您可以使用content_main.xml布局创建片段,并在onCreate()方法中显示。我可以用新片段内容(歌曲列表)替换旧片段(即媒体播放器)。但问题在于歌曲列表片段,操作栏没有显示。由于app_bar_main.xml已打开,因此操作栏仅针对内容_main显示。任何解决方案??您应该尝试用Yaa替换。但是如何在启动应用程序时在片段中显示内容?并将内容放入app_bar_main.xml您可以使用content_main.xml布局创建片段,并在onCreate()方法中显示。我可以用新片段内容(歌曲列表)替换旧片段(即媒体播放器)。但问题在于歌曲列表片段,操作栏没有显示。由于app_bar_main.xml已打开,因此操作栏仅针对内容_main显示。任何解决方案??您应该尝试用Yaa替换。但是如何在启动应用程序时在片段中显示content_main.xml?并将其放入您可以创建的应用程序中