android 重写onNavigationItemSelected将在drawerLayout中单击项时停止导航到片段

jecbmhm3  于 2023-01-19  发布在  Android
关注(0)|答案(1)|浏览(130)

重写onNavigationItemSelected将停止导航组件在单击项时打开片段的功能,而是调用onNavigationItemSelected,而不执行其他操作。
创建时调用了导航视图设置

private fun setupNavigationDrawer(){

        NavigationUI.setupWithNavController(binding.navigationView,navHostFragment.findNavController())
        binding.navigationView.setNavigationItemSelectedListener(this)

    }

活性

class MainActivity : AppCompatActivity(),NavigationView.OnNavigationItemSelectedListener

导航项目选定

override fun onNavigationItemSelected(item: MenuItem): Boolean {
        if(binding.mainActivityRootView.isDrawerOpen(GravityCompat.END)){
            binding.mainActivityRootView.closeDrawer(GravityCompat.END)
        }
        else if(binding.mainActivityRootView.isDrawerOpen(GravityCompat.START)){
            binding.mainActivityRootView.closeDrawer(GravityCompat.START)
        }
        return true

    }

导航视图xml

<com.google.android.material.navigation.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/menu_home_navigation_drawer"
        android:paddingStart="15dp"
        android:paddingEnd="15dp"

        app:headerLayout="@layout/layout_navigation_drawer_header"
        app:itemIconTint="@color/lipstick"
        android:layout_gravity="end"
        app:drawerLayoutCornerSize="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" >

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="bottom"
            android:layout_marginBottom="40dp"
            >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_main_blue"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:tint="@color/lipstick" />

            <TextView
                android:id="@+id/textView41"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/montserratmedium"
                android:text="Version IMPLEMENT"
                android:textColor="@color/pinkish_grey"
                android:textSize="10sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.navigation.NavigationView>

我已将菜单项ID设置为导航片段ID,并且它在未设置onNavigationItemSelected的情况下工作。我还在XML中的片段容器下方具有导航视图
我试过

  • 更改onNavigationItemSelected的返回值-在setupWithNavController之前设置onNavigationItemSelected侦听器,这会导致根本不调用侦听器
zzlelutf

zzlelutf1#

最后我通过手动打开碎片来解决它

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    if(binding.mainActivityRootView.isDrawerOpen(GravityCompat.END)){
        binding.mainActivityRootView.closeDrawer(GravityCompat.END)
    }
    else if(binding.mainActivityRootView.isDrawerOpen(GravityCompat.START)){
        binding.mainActivityRootView.closeDrawer(GravityCompat.START)
    }
    return true

}

override fun onNavigationItemSelected(item: MenuItem): Boolean {
    if(binding.mainActivityRootView.isDrawerOpen(GravityCompat.END)){
        binding.mainActivityRootView.closeDrawer(GravityCompat.END)
    }
    else if(binding.mainActivityRootView.isDrawerOpen(GravityCompat.START)){
        binding.mainActivityRootView.closeDrawer(GravityCompat.START)
    }

    when(item.itemId){
        else ->{
            NavigationUI.onNavDestinationSelected(item,navHostFragment.findNavController())
        }
    }

    return true
}

相关问题