有没有办法让NavigationView减少代码占用(Kotlin)

有没有办法让NavigationView减少代码占用(Kotlin),kotlin,navigation-drawer,Kotlin,Navigation Drawer,我在某种程度上被认为是(Kotlin初学者),目前正在处理一项任务,而我的NavDrawer在每个页面上占用了太多的空间。我已经准备好了自己的标题内容和菜单项,但如果我想在每页中都包含这些内容,则会占用太多空间,每次编辑时都必须更新每个抽屉内容,这将非常耗时!我有没有办法把我所有的NavDarwer代码放在一个地方,然后把它放在其他页面上?感谢您的阅读,并感谢您的建议 编辑- 导航代码: <com.google.android.material.navigation.NavigationV

我在某种程度上被认为是(Kotlin初学者),目前正在处理一项任务,而我的
NavDrawer
在每个页面上占用了太多的空间。我已经准备好了自己的
标题内容
菜单项
,但如果我想在每页中都包含这些内容,则会占用太多空间,每次编辑时都必须更新每个
抽屉
内容,这将非常耗时!我有没有办法把我所有的
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" />
这里的问题是,如果我想在每个页面中添加NavigationView,它将占用大量的代码空间。因此,我想知道是否有任何方法可以让我做一个快捷方式,将整个导航代码放在一个地方,并将其传递到其他页面

最新编辑-

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
}

你能添加一些代码吗?在“编辑-”@saeedentzarisorry之后检查是的,我刚刚意识到,但是所有的祝酒词都给了我错误和意图代码,此外,该项从未调用过,因此我将其与menuItem切换,但仍给出了错误请将其与上下文替换。startActivity中出现错误,它表示不匹配,正在查找意图,但已找到Context@ChipChocolate将startActivity更改为上下文。startActivity(意图)谢谢!:)现在开始工作了,我很抱歉没有很好地理解我对kotlin有点陌生,所以非常感谢
override fun onNavigationItemSelected(item: MenuItem): Boolean {
    NavigationHandler(context, item)()
    drawerLayout.closeDrawer(GravityCompat.START)
    return true
}