导航抽屉android切换betveen片段?

时间:2020-06-21 18:52:28

标签: android navigation-drawer android-jetpack android-architecture-navigation android-jetpack-navigation

好的,是导航图表和数据绑定的新功能,试图遵循android文档上的相关主题,但根本不了解发生了什么。这只是带有导航抽屉的空的活动,但是对于上帝的爱是如何的,当我在导航抽屉中单击某个项目时,我会切换到另一个片段吗?

    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" />

2 个答案:

答案 0 :(得分:1)

如果简单的事情不起作用,我会感到沮丧。

我没有在项目中尝试过您的代码,但是,根据经验,我可以告诉您,除了一件事情,您已经完成了大多数*的设置权限。

app_bar_menu.xml项目ID中,must matchnavigation.xml图中定义的目标ID。

在这里,我分别将现有ID pocetni_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的创建和设置应注意选择导航菜单项并导航至片段。

答案 1 :(得分:1)

在布局中,将位置更改为:

<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