【发布时间】:2020-06-21 18:52:28
【问题描述】:
好的,我是导航图表和数据绑定的新手,尝试关注有关此主题的 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" />
【问题讨论】:
-
发布您的布局
-
@GabrieleMariotti 我发布了我的 main_activity 布局
标签: android navigation-drawer android-jetpack android-architecture-navigation android-jetpack-navigation