单击工具栏搜索打开导航抽屉
Navigation Drawer opening with clicking on toolbar search
我正在开发一个应用程序,该应用程序具有底部导航菜单、导航抽屉和工具栏中的搜索功能。我现在只有一件事在工作——我遇到的问题是当我点击工具栏中的“搜索图标”时,抽屉也打开了!我真的不明白为什么!我敢肯定这是我看不到的简单事情,但是。 . .
无论如何,有人可以帮忙吗!
NavigationActivity.kt
class NavigationActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener{
lateinit var drawerLayout: DrawerLayout
private lateinit var appBarConfiguration: AppBarConfiguration
lateinit var navController: NavController
lateinit var navView: BottomNavigationView
lateinit var myNavView: NavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_navigation)
drawerLayout = findViewById(R.id.drawer_layout)
navView = findViewById(R.id.nav_view)
myNavView = findViewById(R.id.navigation_view) as NavigationView
myNavView.setNavigationItemSelectedListener(this)
navController = findNavController(R.id.nav_host_fragment)
setupBottomNavigation()
appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_map, R.id.navigation_list, R.id.navigation_workmates), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(navController,appBarConfiguration)
}
private fun setupBottomNavigation() {
navView.setupWithNavController(navController)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.search_menu, menu)
val myActionMenuItem = menu!!.findItem(R.id.search)
var searchView = myActionMenuItem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
toast(query)
myActionMenuItem.collapseActionView()
return false
}
override fun onQueryTextChange(s: String): Boolean {
drawerLayout.closeDrawer(GravityCompat.START)
return false
}
})
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
val check = drawerLayout.isDrawerOpen(GravityCompat.START)
if (!check) {
drawerLayout.openDrawer(GravityCompat.START)
} else {
drawerLayout.closeDrawer(GravityCompat.START)
}
return true
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
var selectedOption = ""
when (item?.itemId) {
R.id.yourlunch -> selectedOption = "yourLunch"
R.id.settings -> selectedOption = "settings"
R.id.logout -> selectedOption = "logout"
}
when(selectedOption){
"yourLunch" ->{
toast("Your Lunch Selected")
}
"settings"->{
toast("Settings Selected")
}
"logout" ->{
toast("Logout Selected")
}
}
return super.onOptionsItemSelected(item)
}
}
navigation_drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/yourlunch"
android:title="@string/your_lunch"
android:icon="@drawable/ic_baseline_fastfood_24" />
<item
android:id="@+id/settings"
android:title="@string/settings"
android:icon="@drawable/ic_baseline_settings_24"/>
<item
android:id="@+id/logout"
android:title="@string/logout"
android:icon="@drawable/ic_baseline_power_settings_new_24"/>
</menu>
search_memu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search"
android:title="Search"
android:icon="@drawable/ic_baseline_search_24"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
</menu>
提前感谢您的帮助!
在 onOptionsItemSelected 方法中,您定义了当您单击任务栏中的选项时,它应该打开抽屉菜单。尝试删除它。
如果您想定义要在任务栏上处理的项目,请在 onOptionsItemSelected 中使用下面的用户代码:
when(item.getId()){
R.id.item1 -> ////
R.id.item2 -> ////
}
使用您为菜单项指定的 ID。
问题是您的 onOptionsItemSelected()
没有调用 super.onOptionsItemSelected()
- 那个 是自动调度对 android.R.id.home
按钮的调用并且是什么连接了 onSupportNavigateUp()
调用。
鉴于 onSupportNavigateUp()
正在为您处理向上按钮,您可以删除整个 onOptionsItemSelected()
实现。
我正在开发一个应用程序,该应用程序具有底部导航菜单、导航抽屉和工具栏中的搜索功能。我现在只有一件事在工作——我遇到的问题是当我点击工具栏中的“搜索图标”时,抽屉也打开了!我真的不明白为什么!我敢肯定这是我看不到的简单事情,但是。 . .
无论如何,有人可以帮忙吗!
NavigationActivity.kt
class NavigationActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener{
lateinit var drawerLayout: DrawerLayout
private lateinit var appBarConfiguration: AppBarConfiguration
lateinit var navController: NavController
lateinit var navView: BottomNavigationView
lateinit var myNavView: NavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_navigation)
drawerLayout = findViewById(R.id.drawer_layout)
navView = findViewById(R.id.nav_view)
myNavView = findViewById(R.id.navigation_view) as NavigationView
myNavView.setNavigationItemSelectedListener(this)
navController = findNavController(R.id.nav_host_fragment)
setupBottomNavigation()
appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_map, R.id.navigation_list, R.id.navigation_workmates), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(navController,appBarConfiguration)
}
private fun setupBottomNavigation() {
navView.setupWithNavController(navController)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.search_menu, menu)
val myActionMenuItem = menu!!.findItem(R.id.search)
var searchView = myActionMenuItem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
toast(query)
myActionMenuItem.collapseActionView()
return false
}
override fun onQueryTextChange(s: String): Boolean {
drawerLayout.closeDrawer(GravityCompat.START)
return false
}
})
return true
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
val check = drawerLayout.isDrawerOpen(GravityCompat.START)
if (!check) {
drawerLayout.openDrawer(GravityCompat.START)
} else {
drawerLayout.closeDrawer(GravityCompat.START)
}
return true
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
var selectedOption = ""
when (item?.itemId) {
R.id.yourlunch -> selectedOption = "yourLunch"
R.id.settings -> selectedOption = "settings"
R.id.logout -> selectedOption = "logout"
}
when(selectedOption){
"yourLunch" ->{
toast("Your Lunch Selected")
}
"settings"->{
toast("Settings Selected")
}
"logout" ->{
toast("Logout Selected")
}
}
return super.onOptionsItemSelected(item)
}
}
navigation_drawer_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/yourlunch"
android:title="@string/your_lunch"
android:icon="@drawable/ic_baseline_fastfood_24" />
<item
android:id="@+id/settings"
android:title="@string/settings"
android:icon="@drawable/ic_baseline_settings_24"/>
<item
android:id="@+id/logout"
android:title="@string/logout"
android:icon="@drawable/ic_baseline_power_settings_new_24"/>
</menu>
search_memu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search"
android:title="Search"
android:icon="@drawable/ic_baseline_search_24"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
</menu>
提前感谢您的帮助!
在 onOptionsItemSelected 方法中,您定义了当您单击任务栏中的选项时,它应该打开抽屉菜单。尝试删除它。 如果您想定义要在任务栏上处理的项目,请在 onOptionsItemSelected 中使用下面的用户代码:
when(item.getId()){
R.id.item1 -> ////
R.id.item2 -> ////
}
使用您为菜单项指定的 ID。
问题是您的 onOptionsItemSelected()
没有调用 super.onOptionsItemSelected()
- 那个 是自动调度对 android.R.id.home
按钮的调用并且是什么连接了 onSupportNavigateUp()
调用。
鉴于 onSupportNavigateUp()
正在为您处理向上按钮,您可以删除整个 onOptionsItemSelected()
实现。