Skip to content

Instantly share code, notes, and snippets.

@taktamur
Last active May 24, 2020 06:18
Show Gist options
  • Save taktamur/990b22be1bd871bbfb970387a47a2d05 to your computer and use it in GitHub Desktop.
Save taktamur/990b22be1bd871bbfb970387a47a2d05 to your computer and use it in GitHub Desktop.
Screapboxから:Android menuリソース

Androidの上のバー部分に、メニューを追加する

// 右上のメニューの組み立て
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.list_view_menu, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
return when(item?.itemId){
R.id.menuItem1 -> {
// adapterにinsertしたら画面が更新される
// これはinserの中で、notifyDataSetChanged()が呼ばれているから
adapter.insert("ADD"+adapter.count,0)
true
}
R.id.menuItem2 -> {
adapter.remove(adapter.getItem(0))
true
}
else->{
super.onOptionsItemSelected(item)
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:compat="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menuItem1"
compat:showAsAction="ifRoom"
android:title="追加" />
<item
android:id="@+id/menuItem2"
compat:showAsAction="always"
android:title="削除" />
</menu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment