ActionBar에 view를 배치하고 접었다 펼쳤다 할 수 있는 개념
app:showAsAction="always|collapseActionView"
app:actionViewClass="androidx.appcompat.widget.SearchView"
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.main_menu,menu)
return super.onCreateOptionsMenu(menu)
}
val itemSearch = menu?.findItem(R.id.item1)
val searchView = itemSearch?.actionView as SearchView
searchView.queryHint = "검색어 입력"
ActionView가 펼쳐지거나 접혔을 때 동작하는 listener
itemSearch.setOnActionExpandListener(object : MenuItem.OnActionExpandListener{
override fun onMenuItemActionExpand(p0: MenuItem): Boolean {
return true
}
override fun onMenuItemActionCollapse(p0: MenuItem): Boolean {
return true
}
})
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
searchView.clearFocus()
itemSearch.collapseActionView()
return true
}
override fun onQueryTextChange(newText: String?): Boolean {
return true
}
val data1 = arrayOf(
"데이터1", "데이터2", "데이터3", "data1", "data2"
)
activityMainBinding.run{
list.run{
adapter = ArrayAdapter<String>(
this@MainActivity, android.R.layout.simple_list_item_1, data1
)
}
}
isTextFilterEnabled = true
activityMainBinding.list1.setFilterText(newText)
if(newText?.length == 0){
activityMainBinding.list1.clearTextFilter()
}
list.setOnItemClickListener { adapterView, view, i, l ->
val str = adapterView.adapter.getItem(i) as String
val idx = data1.indexOf(str)
activityMainBinding.textView.text = data1[idx]
}
supportActionBar?.setHomeButtonEnabled(true)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeAsUpIndicator(R.mipmap.ic_launcher)
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when(item.itemId) {
android.R.id.home -> {
finish()
}
}
return super.onOptionsItemSelected(item)
}