我正在学习Android studio,并学到了一些关于导航抽屉的知识。 后来我将我的安卓工作室更新到 3.5 版本 现在我看到旧代码消失了。 当我是新学习者时,我看到的新代码我完全困惑这个新代码如何用于片段, 是的,我尝试过搜索,但没有一个有效 有人可以帮助我完成以下代码吗?
public class HomeActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
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, R.id.nav_gallery, R.id.nav_slideshow,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.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.home, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
我想在单击菜单后打开相关的片段。
来自文件 xml>>activity_main.xml 查看默认顺序
<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" />
更改为以下顺序:
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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" />
在 Java 中使用
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull
NavDestination destination, @Nullable Bundle arguments) {
if (destination.getId() == R.id.nav_home) {
Toast.makeText(MainActivity.this, "home", Toast.LENGTH_LONG).show();
}
if (destination.getId() == R.id.nav_gallery) {
Toast.makeText(MainActivity.this, "nav_gallery", Toast.LENGTH_LONG).show();
}
if (destination.getId() == R.id.nav_slideshow) {
Toast.makeText(MainActivity.this, "nav_slideshow", Toast.LENGTH_LONG).show();
}
if (destination.getId() == R.id.nav_tools) {
Toast.makeText(MainActivity.this, "nav_tools", Toast.LENGTH_LONG).show();
}
if (destination.getId() == R.id.nav_share) {
Toast.makeText(MainActivity.this, "nav_share", Toast.LENGTH_LONG).show();
}
if (destination.getId() == R.id.nav_send) {
Toast.makeText(MainActivity.this, "nav_send", Toast.LENGTH_LONG).show();
}
}
});