Skip to content

Instantly share code, notes, and snippets.

@hiroyuki-seto
Created November 10, 2016 12:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiroyuki-seto/c782cf30a9899f84e56559daf492b4fc to your computer and use it in GitHub Desktop.
Save hiroyuki-seto/c782cf30a9899f84e56559daf492b4fc to your computer and use it in GitHub Desktop.
Android Nで変わったContextMenuまわり

Android Nで変わったContextMenuまわり

知りたかった点

  • Nでは長押しした位置にContextMenuが出るがどうやっているか
  • View#performLongClick(int,int)View#showContextMenu(int,int)が追加されている
  • View#performLongClickInternalで指の位置があるかによってshowContextMenu(int,int)showContextMenu()を呼び分け
  • NではDarkThemeにするとPopupWindowが黒背景になるが、どうにかならんのか
  • M以前はDialogだったのでテーマを書き換えられた。NはPopupかつcom.android.internal.R.attr.contextPopupMenuStyleを使っているのできびしそう?

追ってみた

  • View#performLongClick(int,int)
  • View#performLongClickInternal(int,int)
  • View#showContextMenu(int,int)
  • ViewGroup#showContextMenuForChild(View,float,float)
  • AbsListView#showContextMenuForChild(View,float,float)
  • AbsListView#showContextMenuForChildInternal(View,float,float,boolean)
  • ViewGroup#showContextMenuForChild(View,float,float)
  • mParent.showContextMenuForChild(View,float,float)でどこまで遡るかと思ったらDecorView#showContextMenuForChild(View,float,float)
  • DecorView#showContextMenuForChildInternal(View,float,float)
  • ContextMenuBuilder#showPopup(Context,View,float,float)
  • MenuPopupHelper#show(int,int)
  • MenuPopupHelper#tryShow(int,int)
  • MenuPopupHelper#showPopup(int,int,boolean,boolean)
  • MenuPopup#show()
  • 🎉

気づいたこと

    public boolean performLongClick(float x, float y) {
        mLongClickX = x;
        mLongClickY = y;
        final boolean handled = performLongClick();
        mLongClickX = Float.NaN;
        mLongClickY = Float.NaN;
        return handled;
    }
        if (isPopup) {
            helper = mWindow.mContextMenu.showPopup(getContext(), originalView, x, y);
         } else {
             helper = mWindow.mContextMenu.showDialog(originalView, originalView.getWindowToken());
         }
// Set the transition epicenter to be roughly finger (or mouse
// cursor) sized and centered around the offset position. This
// will give the appearance that the window is emerging from
// the touch point.
  • v7にもMenuPopupHelperがあるので、M以前でもタップ位置からContextMenu出せるかも?
  • 調べてみたらActionMenu用のようだ
  • ContextMenuで流用するのもそんなにつらくないかも
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment