gpt4 book ai didi

Android:导航组件不能同时使用: 和 NavigationItemSelectedListener

转载 作者:行者123 更新时间:2023-11-30 04:54:51 24 4
gpt4 key购买 nike

我做了什么:

我创建了Navigation Drawer Activity,更新了Navigation Drawer Activity的新格式,根据新的Android架构,我用 Navigation Component得到了它结构。

NavigationView代码 NavControllerNavigationUI如下所示,当我点击任何导航项时打开 fragment 。

    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_profile, R.id.nav_privacy_policy,
R.id.nav_terms, R.id.nav_contact_us, 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);

这是给 nav_host_fragment 的:

<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />

导航正在使用这个 navigation/mobile_navigation.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
android:id="@+id/nav_home"
android:name="com.sohamerp.marsremedies.fragment.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />

<fragment
android:id="@+id/nav_profile"
android:name="com.sohamerp.marsremedies.fragment.ProfileFragment"
android:label="@string/menu_my_profile"
tools:layout="@layout/fragment_profile" />

<fragment
android:id="@+id/nav_privacy_policy"
android:name="com.sohamerp.marsremedies.fragment.PrivacyPolicyFragment"
android:label="@string/menu_privacy_policy"
tools:layout="@layout/fragment_privacy_policy" />

<fragment
android:id="@+id/nav_terms"
android:name="com.sohamerp.marsremedies.fragment.TermsConditionFragment"
android:label="@string/menu_terms"
tools:layout="@layout/fragment_terms_condition" />

<fragment
android:id="@+id/nav_contact_us"
android:name="com.sohamerp.marsremedies.fragment.ContactUsFragment"
android:label="@string/menu_contact_us"
tools:layout="@layout/fragment_terms_condition" />

</navigation>

我遇到问题的地方:

Android Studio 已为所有 6 个菜单创建 fragment ,但我不想在单击最后两项时打开 fragment 。 为此,我删除了最后两个 <fragment>来自 mobile_navigation.xml 的标签

enter image description here

我尝试添加下面的 setNavigationItemSelectedListener

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
if (menuItem.getItemId() == R.id.nav_share)
Toast.makeText(NavigationMenuActivity.this, "Sharing...", Toast.LENGTH_SHORT).show();
else if (menuItem.getItemId() == R.id.nav_send)
Toast.makeText(NavigationMenuActivity.this, "Rating...", Toast.LENGTH_SHORT).show();
return false;
}
});

当我点击最后两个菜单但前 4 个菜单不起作用时,Toast 正在显示。

我该怎么做才能让它发挥作用?

build.gradle 依赖:

implementation 'androidx.navigation:navigation-fragment:2.1.0'
implementation 'androidx.navigation:navigation-ui:2.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'

最佳答案

您可以创建一个 switch 语句,对于其他 4 个 fragment 您应该返回 true

    switch(menuItem.getItemId) {
case R.id.import:
navController.navigate(R.id.nav_home)
return true
case R.id.nav_gallery:
navController.navigate(R.id.nav_gallery)
return true
case R.id.nav_slideshow:
navController.navigate(R.id.nav_slideshow)
return true
case R.id.nav_tools:
navController.navigate(R.id.nav_tools)
return true
case R.id.nav_share:
Toast.makeText(NavigationMenuActivity.this, "Sharing...", Toast.LENGTH_SHORT).show();
return false;
case R.id.nav_send:
Toast.makeText(NavigationMenuActivity.this, "Rating...",Toast.LENGTH_SHORT).show();
return false;
default:
return false;
}
}

通过重写 onBackPressed,您可以实现默认行为。 (注意:默认行为只是返回到第一个 fragment ,然后在其关闭 Activity 之后)

public void onBackPressed() {

 if (navigationDrawer.isDrawerOpen(GravityCompat.START)) {
navigationDrawer.closeDrawer(GravityCompat.START)
} else {
if (navController.currentDestination?.id == R.id.nav_home){
finish()
} else {
navController.navigate(R.id.action_global_nav_home)
drawerView.menu.findItem(R.id.menu_item_nav_home).isChecked = true
}

并在 navigation/mobile_navigation.xml 中添加一个全局操作:

 <action
android:id="@+id/action_global_nav_home"
app:destination="@id/nav_home" />

关于Android:导航组件不能同时使用:<navigation> 和 NavigationItemSelectedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59434942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com