0

I would like to open a "contextual menu" when a user touches a button.

I have searched with no success for a solution to have the default android context menu without autofocus. To me the default autofocus is a pain in the neck as it forces extra taps for the user (out of the menu window) to select another item on the main window.

My activity offers a gridview with items g1, g2,... and the contextual menu would display a list of textareas t1, t2... Here is what I need:

  1. when user taps g1, a menu shows up at the bottom of the screen (I need to be able to customize the menu layout basically)
  2. when user taps g1, the rest of the window does not fade away i.e. he should still be able to view normally the main window except for the space taken by the menu at the bottom
  3. when user taps g1, he does not loose the focus of the current window i.e. if he taps g2 after the menu has open, g2 will act normally without requiring an extra tap
  4. let's say user taps g1, then taps g2: the current contextual menu should close and a new one opens, refreshed for b2
  5. i need to do that for apps starting from minsdkversion=8 (seems i cannot use "action bar")

Hope this makes sense, let me know if it doesn't.

I guess that would be too much hassle to hack the default context menu to customize it this way? It's totally ok to create my own. But I don't know where to start. So could you point me toward the direction of achieving what I want ?

Thanks for your time

4

1 回答 1

0

Two suggestions how to do that with self-defined context menus.

Variant A: Maybe it is an option for you to wrap your current Layout into a RelativeLayout, and add a LinearLayout (containing your self-defined context menu) to that RelativeLayout as soon as the user clicks an g1. When g2 is clicked, remove that old LinearLayout and add a new one with relativeLayout.addView(contextMenuLayout). To have your LinearLayout placed on the bottom, make sure to use contextMenuLayout.setLayoutParams(...) on it and pass an instance of RelativeLayout.LayoutParams, and set Align Parent Bottom and Center Horizontal. Because your contextMenuLayout is the last element of the RelativeLayout, it will be drawn on top. Focus will act as you wish.

Variant B: Wrap your current Layout into a LinearLayout with vertical orientation. The first cell contains your current layout. The second cell may contain your self-defined context menu, you add and remove just like in Variant A. Two advantages: first, your original layout is not covered by the context view, and in that way everything is always visible. Secondly, you can set the 'LinearLayout' to "animateLayoutChanges", which makes the context menu fade in smoothly with Android API11+ (ignored in earlier versions).

Variant A and B as Pseudocode (both work similar):

class YourActivity extends Activity implements OnItemClickListener {

    View contextMenu = null;

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        // this could be RelativeLayout (Variant A) or LinearLayout (Variant B) which in turn contains your Layout
        Layout rootLayout = (Layout)findViewById(R.id.root_layout);

        // in case there is a context menu open, remove it
        if(contextMenu!=null)
            rootLayout.removeView(contextMenu);

        // add new self-defined context menu
        contextMenu = getContextMenuView();
        rootLayout.addView(contextMenu);
    }

    private View getContextMenuView() {
        Layout contextMenuView = new LinearLayout(...);
        .... add contents ...
        return contextMenuView;
    }

    ....
}

Note you need to cast rootLayout to either LinearLayout or RelativeLayout, since Layout itself has no addView(...) method.

于 2012-12-21T12:36:59.890 回答