0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【kotlin】コンテキストメニュー

Posted at

■ コンテキストメニュー

リストビューを長押しタップすると
新たなメニューが表示される。

1

コンテキストメニュー用の.xmlファイルを作成する。

resフォルダ右クリック
[New] -> [Android Resource Directory]
専用画面出てくるので
Resource type: menu に変更。
OK

[menu]フォルダ作成
[New] -> [Menu Resource File]
作成専用画面で[File Nmae]好きな名前
[OK]

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/menuListContextDesc"
        android:title="@string/menu_desc"/>

    <item
        android:id="@+id/menuListContextOrder"
        android:title="@string/menu_order"/>

</menu>

オプションメニューと違いshowAsActionはいらない。

2

表示したいアクティビティに記述する。

■overrideしてonCreateContextMenu()を実装
親クラスのメソッドを呼び出す。

override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
        super.onCreateContextMenu(menu, v, menuInfo)
    }

3

コンテキストメニューを表示

override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
        super.onCreateContextMenu(menu, v, menuInfo)
       //表示したいメニューファイルをR値で渡す
       menuIntflater.inflate(R.menu.作成したContextMenuの名前,menu)
    }

4

コンテキストメニューのヘッダタイトルを設定

その後
onCreate()に
registerForContextMenu(リストビュー)
で画面部品を登録

override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
        super.onCreateContextMenu(menu, v, menuInfo)
       menuIntflater.inflate(R.menu.作成したContextMenuの名前,menu)
       //タイトルを表示する。
       menu?.setHeaderTitle(タイトルに表示したいstringR)
    }

override fun onCreate(savedInstanceState: Bundle?) {

   省略
  registerForContextMenu(リストビュ)
}


5

item選択時の処理

■引数のitemのmenuInfoプロパティをAdapterView.AdapterContextMenuInfo型にキャストする
長押しされたビューに関する情報
■AdapterContextMenuInfoオブジェクトにpositionプロパティがありどの行を選択したかの値が格納されている。
長押しされたリストのポジション

override fun onContextItemSelected(item: MenuItem): Boolean {
  //戻り値用の変数
  var returnVal = true
  //長押しされたビューに関する情報
  val info = item.menuInfo as AdapterView.AdapterContetxMenuInfo
  //長押しされたリストのポジション
  val listPosition = info.position
}

6

長押しされたポジションのメニュー情報を取得

override fun onContextItemSelected(item: MenuItem): Boolean {
  //戻り値用の変数
  var returnVal = true
  val info = item.menuInfo as AdapterView.AdapterContetxMenuInfo
  val listPosition = info.position
  //プロパティで用意した_menuListのポジションとして変数menuに入れる。
  val menu = _menuList[listPosition]
}

7

when文で処理を分岐する。

override fun onContextItemSelected(item: MenuItem): Boolean {
  //戻り値用の変数
  var returnVal = true
  val info = item.menuInfo as AdapterView.AdapterContetxMenuInfo
  val listPosition = info.position
  val menu = _menuList[listPosition]

  when(item.itemId) {
    ContextMenuitemR -> 処理
    ContextMenuitemR -> 処理
    else ->
     returnVal = super.onContextItemSelected(item)
  }
  return returnVal
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?