SearchView 未出现在浮动操作按钮上单击
SearchView not appearing on Floating Action Button click
如何从浮动操作按钮打开 SearchView
小部件?似乎没有明显的方法可以在菜单项之外打开它,这似乎很奇怪。我尝试使用 val searchView = search.expandActionView(search)
但没有用。
class MainActivity: AppCompatActivity() {
private lateinit var mAdapter: AdapterServiceUpdates
private val arrayItems = ArrayList<ItemCompany>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.collapsingtoolbar_and_recyclerview)
val mCollapsingToolbarLayout = findViewById<CollapsingToolbarLayout>(R.id.myCollapsingToolbarLayout)
val mToolbar = findViewById<Toolbar>(R.id.myToolbar)
val mRecyclerView = findViewById<RecyclerView>(R.id.myRecyclerView)
setSupportActionBar(mToolbar)
mCollapsingToolbarLayout.title = "MainActivity"
mToolbar.setNavigationOnClickListener { onBackPressed() }
val mFab: View = findViewById(R.id.myFab)
mFab.setOnClickListener { view -> ?
}
arrayItems.add(ItemCompany("Item A"))
arrayItems.add(ItemCompany("Item B"))
arrayItems.add(ItemCompany("Item C"))
arrayItems.add(ItemCompany("Item D"))
arrayItems.add(ItemCompany("Item E"))
arrayItems.add(ItemCompany("..."))
mRecyclerView.layoutManager =
androidx.recyclerview.widget.LinearLayoutManager(this)
val mListener = AdapterView.OnItemClickListener { _, _, _, _ -> }
mAdapter = MyAdapter(this, arrayItems, true)
mRecyclerView.addItemDecoration(
androidx.recyclerview.widget.DividerItemDecoration(
this,
LinearLayout.VERTICAL
)
)
mRecyclerView.adapter = mAdapter
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_search, menu)
val search = menu.findItem(R.id.action_search)
val searchView = search.expandActionView(search)
// val searchView = search.actionView as SearchView
searchView.queryHint = "Search"
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
mAdapter.filter.filter(newText)
return true
}
})
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
val intent = parentActivityIntent
intent?.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
onBackPressed()
true
}
else ->
super.onOptionsItemSelected(item)
}
}
}
您可以在 FAB OnClickListener
回调中使用 searchView.setIconified(false)
打开 SearchView
How can I do it on class level when Android is expecting it to be
declared as an options menu item?
您可以在 class 级别使用 lateinit 变量,如下所示(注意用 //<<<<<<< change here
符号
标记的更改
class MainActivity: AppCompatActivity() {
lateinit var searchView: SearchView //<<<<<<< change here
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.collapsingtoolbar_and_recyclerview)
// rest of your code .....
val mFab: View = findViewById(R.id.myFab)
mFab.setOnClickListener { view -> ?
searchView?.setIconified(false) //<<<<<<< change here
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_search, menu)
val searchItem: MenuItem = menu.findItem(R.id.action_search)
searchView = searchItem.getActionView() as SearchView ///<<<<<<< change here
// ....
// .... rest of onCreateOptionsMenu() method
我给你的建议是
//class level
private val fab
get() = findViewById(R.id.fab)
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.search_menu, menu)
val searchItem: MenuItem = menu.findItem(R.id.action_search)
if (searchItem != null) {
searchView = MenuItemCompat.getActionView(searchItem) as SearchView
fab.setOnClickListener{_->
searchView.setIconified(false)
}
//....
}
试试这个来扩展 SearchView
searchView.setIconified(false)
并折叠它
searchView.setIconified(true)
重要!
您应该将 android:showAsAction 的值从 ifRoom|collapseActionView 更改为 always
希望对你有所帮助
如何从浮动操作按钮打开 SearchView
小部件?似乎没有明显的方法可以在菜单项之外打开它,这似乎很奇怪。我尝试使用 val searchView = search.expandActionView(search)
但没有用。
class MainActivity: AppCompatActivity() {
private lateinit var mAdapter: AdapterServiceUpdates
private val arrayItems = ArrayList<ItemCompany>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.collapsingtoolbar_and_recyclerview)
val mCollapsingToolbarLayout = findViewById<CollapsingToolbarLayout>(R.id.myCollapsingToolbarLayout)
val mToolbar = findViewById<Toolbar>(R.id.myToolbar)
val mRecyclerView = findViewById<RecyclerView>(R.id.myRecyclerView)
setSupportActionBar(mToolbar)
mCollapsingToolbarLayout.title = "MainActivity"
mToolbar.setNavigationOnClickListener { onBackPressed() }
val mFab: View = findViewById(R.id.myFab)
mFab.setOnClickListener { view -> ?
}
arrayItems.add(ItemCompany("Item A"))
arrayItems.add(ItemCompany("Item B"))
arrayItems.add(ItemCompany("Item C"))
arrayItems.add(ItemCompany("Item D"))
arrayItems.add(ItemCompany("Item E"))
arrayItems.add(ItemCompany("..."))
mRecyclerView.layoutManager =
androidx.recyclerview.widget.LinearLayoutManager(this)
val mListener = AdapterView.OnItemClickListener { _, _, _, _ -> }
mAdapter = MyAdapter(this, arrayItems, true)
mRecyclerView.addItemDecoration(
androidx.recyclerview.widget.DividerItemDecoration(
this,
LinearLayout.VERTICAL
)
)
mRecyclerView.adapter = mAdapter
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_search, menu)
val search = menu.findItem(R.id.action_search)
val searchView = search.expandActionView(search)
// val searchView = search.actionView as SearchView
searchView.queryHint = "Search"
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
mAdapter.filter.filter(newText)
return true
}
})
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
val intent = parentActivityIntent
intent?.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
onBackPressed()
true
}
else ->
super.onOptionsItemSelected(item)
}
}
}
您可以在 FAB OnClickListener
回调中使用 searchView.setIconified(false)
打开 SearchView
How can I do it on class level when Android is expecting it to be declared as an options menu item?
您可以在 class 级别使用 lateinit 变量,如下所示(注意用 //<<<<<<< change here
符号
class MainActivity: AppCompatActivity() {
lateinit var searchView: SearchView //<<<<<<< change here
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.collapsingtoolbar_and_recyclerview)
// rest of your code .....
val mFab: View = findViewById(R.id.myFab)
mFab.setOnClickListener { view -> ?
searchView?.setIconified(false) //<<<<<<< change here
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_search, menu)
val searchItem: MenuItem = menu.findItem(R.id.action_search)
searchView = searchItem.getActionView() as SearchView ///<<<<<<< change here
// ....
// .... rest of onCreateOptionsMenu() method
我给你的建议是
//class level
private val fab
get() = findViewById(R.id.fab)
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.search_menu, menu)
val searchItem: MenuItem = menu.findItem(R.id.action_search)
if (searchItem != null) {
searchView = MenuItemCompat.getActionView(searchItem) as SearchView
fab.setOnClickListener{_->
searchView.setIconified(false)
}
//....
}
试试这个来扩展 SearchView
searchView.setIconified(false)
并折叠它
searchView.setIconified(true)
重要!
您应该将 android:showAsAction 的值从 ifRoom|collapseActionView 更改为 always
希望对你有所帮助