kotlin - 有没有办法让 NavigationView 占用更少的代码(Kotlin)
问题描述
我在某种程度上被认为是(Kotlin 初学者)并且目前正在处理一项任务,并且我NavDrawer
在每个页面中占用了太多空间。我有我自己的Header content
并且Menu 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" />
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,它会占用大量代码空间。所以我想知道是否有任何方法可以创建一个快捷方式将整个导航代码放在一个地方并将其传递给其他页面。
最新编辑-
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
}
推荐阅读
- python - 根据变量设置 Pandas 数据框的列值
- python - sqlite python - 通过将现有表中的值相乘来创建新表
- java - 使用 addWindowListener 的困难
- jquery - 如何在 JsTree 中设置关闭未选择的节点?
- php - 检查最新的更新数据,仅更新日期在另一个查询中运行
- java - 如何从firebase检索数据并输入到csv
- php - My ajax delete is not passing data to the controller. Codeigniter
- xmpp - Tigase Custom Database
- php - 如何在php中将短语转换为1个单词
- excel-formula - SUMIFS 语法问题