有没有办法让导航视图占用更少的代码(Kotlin)



我在某种程度上被认为是一个(Kotlin 初学者(并且目前正在处理一项任务,我的NavDrawer在每个页面中占用了太多空间。我有自己的Header contentMenu Items准备好了,但如果我想将其包含在每个页面中,它会占用太多空间,并且每次编辑时都必须更新每个drawer内容将非常耗时!有什么方法可以让我在一个地方保存我所有的NavDarwer代码,然后将其包含在其他页面中?感谢您的阅读并感谢任何建议

编辑-

导航代码:

<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"
app:itemIconTint="@color/colorPrimary"
app:menu="@menu/nav_menu" />

用于导航的主活动中的代码:

override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.nav_latestAds -> {
Toast.makeText(this, "You are currently here", Toast.LENGTH_SHORT).show()
}
R.id.nav_cars -> {
Toast.makeText(this, "Messages clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_properties -> {
Toast.makeText(this, "Friends clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_mobiles -> {
Toast.makeText(this, "Update clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_electDev -> {
Toast.makeText(this, "Sign out clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_furniture -> {
Toast.makeText(this, "Sign out clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_customerReg -> {
Toast.makeText(this, "Sign out clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_vendorReg -> {
val intent = Intent(this, RegisterVendor::class.java)
startActivity(intent)
}
R.id.nav_logout -> {
if (AuthService.isLoggedIn) {
UserDataService.logout()
Menu_userName.text = "User name"
} else {
val loginIntent = Intent(this, Login::class.java)
startActivity(loginIntent)
}
}
}
drawerLayout.closeDrawer(GravityCompat.START)
return true
}

这里的问题是,如果我想在每个页面中添加导航视图,它将占用大量代码空间。所以我想知道是否有任何方法可以制作一个快捷方式,将整个导航代码放在一个地方并将其传递给其他页面。

最新编辑-

package com.example.wasit
import android.content.Context
import android.content.Intent
import android.view.MenuItem
import android.widget.Toast
import androidx.core.content.ContextCompat.startActivity
import com.example.wasit.Model.Login
import com.example.wasit.Model.RegisterVendor
import com.example.wasit.Services.AuthService
import com.example.wasit.Services.UserDataService
class NavigationHandler (val context: Context,val menuItem: MenuItem){
operator fun invoke() {
when (item.itemId) {
R.id.nav_latestAds -> {
Toast.makeText(context, "You are currently here", Toast.LENGTH_SHORT).show()
}
R.id.nav_cars -> {
Toast.makeText(context, "Messages clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_properties -> {
Toast.makeText(context, "Friends clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_mobiles -> {
Toast.makeText(context, "Update clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_electDev -> {
Toast.makeText(context, "Sign out clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_furniture -> {
Toast.makeText(context, "Sign out clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_customerReg -> {
Toast.makeText(context, "Sign out clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_vendorReg -> {
val intent = Intent(context, RegisterVendor::class.java)
startActivity(intent)
}
R.id.nav_logout -> {
if (AuthService.isLoggedIn) {
UserDataService.logout()
Menu_userName.text = "User name"
} else {
val loginIntent = Intent(context, Login::class.java)
startActivity(loginIntent)
}
}
}
}
}

将代码移动到另一个类,只需传递它所需的依赖项:

class NavigationHandler(val context: Context, val menuItem: MenuItem){
operator fun invoke() {
when (item.itemId) {
R.id.nav_latestAds -> {
Toast.makeText(this, "You are currently here", Toast.LENGTH_SHORT).show()
}
R.id.nav_cars -> {
Toast.makeText(this, "Messages clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_properties -> {
Toast.makeText(this, "Friends clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_mobiles -> {
Toast.makeText(this, "Update clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_electDev -> {
Toast.makeText(this, "Sign out clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_furniture -> {
Toast.makeText(this, "Sign out clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_customerReg -> {
Toast.makeText(this, "Sign out clicked", Toast.LENGTH_SHORT).show()
}
R.id.nav_vendorReg -> {
val intent = Intent(this, RegisterVendor::class.java)
startActivity(intent)
}
R.id.nav_logout -> {
if (AuthService.isLoggedIn) {
UserDataService.logout()
Menu_userName.text = "User name"
} else {
val loginIntent = Intent(this, Login::class.java)
startActivity(loginIntent)
}
}
}
}
}

并像这样称呼它:

override fun onNavigationItemSelected(item: MenuItem): Boolean {
NavigationHandler(context, item)()
drawerLayout.closeDrawer(GravityCompat.START)
return true
}

最新更新