有没有办法让 NavigationView 占用更少的代码(Kotlin)
is there a way of making NavigationView take less code (Kotlin)
我在某种程度上被认为是(Kotlin 初学者)并且目前正在处理一项任务,而我的 NavDrawer
在每页中占用了太多 space。我已经准备好自己的 Header content
和 Menu Items
,但是如果我想将它包含在每个页面中,它需要太多 space 并且每次编辑时都必须更新每个 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" />
导航的MainActivity中的代码:
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
}
这里的问题是,如果我想在每个页面中添加 NavigationView,将需要大量代码 space。所以我想知道是否有任何方法可以创建一个快捷方式,将整个导航代码放在一个地方并将其传递给其他页面。
最新编辑-
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 并只传递它需要的依赖项:
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
}
我在某种程度上被认为是(Kotlin 初学者)并且目前正在处理一项任务,而我的 NavDrawer
在每页中占用了太多 space。我已经准备好自己的 Header content
和 Menu Items
,但是如果我想将它包含在每个页面中,它需要太多 space 并且每次编辑时都必须更新每个 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" />
导航的MainActivity中的代码:
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
}
这里的问题是,如果我想在每个页面中添加 NavigationView,将需要大量代码 space。所以我想知道是否有任何方法可以创建一个快捷方式,将整个导航代码放在一个地方并将其传递给其他页面。
最新编辑-
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 并只传递它需要的依赖项:
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
}