导航抽屉 android 在片段之间切换?

Navigation drawer android switching betveen fragments?

好的,我是导航图和数据绑定方面的新手,尝试遵循有关此主题的 android 文档,但根本无法理解发生了什么 on.This 完全是空的 activity使用导航抽屉,但是,看在上帝的份上,当我点击导航抽屉中的某个项目时,我如何切换到另一个片段?

    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home_fragment, R.id.nav_gallery_fragment, R.id.nav_slideshow_fragment)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

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

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.pocetni_ekran: {
                Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.nav_home_fragment);
                break;
            }
            case R.id.galerija: {
                Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.nav_gallery_fragment);
                break;
            }
            case R.id.slajdsou: {
                Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.nav_slideshow_fragment);
                break;
            }
        }
        item.setChecked(true);
return true;
    }
}

还有我的手机导航xml

    <?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
    app:startDestination="@+id/nav_home_fragment">

    <fragment
        android:id="@+id/nav_home_fragment"
        android:name="com.nswd.slajdmeni.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home">

        <action
            android:id="@+id/action_HomeFragment_to_HomeSecondFragment"
            app:destination="@id/nav_gallery_fragment" />
        <action
            android:id="@+id/action_nav_home_fragment_to_nav_slideshow_fragment"
            app:destination="@id/nav_slideshow_fragment" />
    </fragment>



    <fragment
        android:id="@+id/nav_gallery_fragment"
        android:name="com.nswd.slajdmeni.ui.gallery.GalleryFragment"
        android:label="@string/menu_gallery"
        tools:layout="@layout/fragment_gallery" />

    <fragment
        android:id="@+id/nav_slideshow_fragment"
        android:name="com.nswd.slajdmeni.ui.slideshow.SlideshowFragment"
        android:label="@string/menu_slideshow"
        tools:layout="@layout/fragment_slideshow" />
</navigation>

和我的应用栏 xml

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

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/pocetni_ekran"
            android:icon="@drawable/ic_menu_camera"
            android:title="@string/menu_home" />
        <item
            android:id="@+id/galerija"
            android:icon="@drawable/ic_menu_gallery"
            android:title="@string/menu_gallery" />
        <item
            android:id="@+id/slajdsou"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="@string/menu_slideshow" />
    </group>
</menu>

已编辑: 发布我的 main_activity 布局:

<androidx.drawerlayout.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">

<com.google.android.material.navigation.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_main"
    app:menu="@menu/activity_main_drawer" />

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

我理解如果一些简单的事情不起作用时的挫败感。

我没有在项目中尝试过你的代码,但是,根据经验我可以看出你已经完成了大部分*设置,除了一件事。

在您的 app_bar_menu.xml 项目 ID 中,must match 在您的 navigation.xml 图表中定义的目的地 ID。

这里我将已有的IDpocetni_ekran galerija slajdsou 分别替换为nav_home_fragmentnav_gallery_fragmentnav_slideshow_fragment

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home_fragment"
            android:icon="@drawable/ic_menu_camera"
            android:title="@string/menu_home" />
        <item
            android:id="@+id/nav_gallery_fragment"
            android:icon="@drawable/ic_menu_gallery"
            android:title="@string/menu_gallery" />
        <item
            android:id="@+id/nav_slideshow_fragment"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="@string/menu_slideshow" />
    </group>
</menu>

完成后,您可以完全删除 onNavigationItemSelected(@NonNull MenuItem item) 块。因为 mAppBarConfiguration 创建和设置应该负责选择导航菜单项并导航到片段。

在您的布局中将位置更改为:

<androidx.drawerlayout.widget.DrawerLayout>

  <include
    .../>

  <com.google.android.material.navigation.NavigationView
    android:layout_gravity="start"
    ../>

</androidx.drawerlayout.widget.DrawerLayout>

然后替换 activity_main_drawer 中的 ID 以匹配 navigation.xml 图中定义的 ID