开发者

Android: ExpandableListView and context menus

I'm writing an app utilizing ExpandableListView开发者_开发知识库.

I use a subclass of BaseExpandableListAdapter to provide data to my ExpandableListView.

Then I try to add context menus to this view, but these menus should be different for group an child items in the expandable list. So in my adapter's getChildView and getGroupView I call setOnCreateContextMenuListener like this:

TextView textView = new TextView(TestActivity.this);;
textView.setText(getChild(groupPosition, childPosition).toString());      
textView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
      @Override
      public void onCreateContextMenu(ContextMenu menu, View v,
                                      ContextMenu.ContextMenuInfo menuInfo) {
        TestActivity.this.onCreateChildContextMenu(menu, v, menuInfo);
      }
    });
return textView;

TestActivity.this.onCreateChildContextMenu simply adds a few string to menu.

The problem:

This works quite fine - context menu appears and works as expected but group item cannot be expanded anymore. It simply ignores short clicks. I've checked - as expected the textView becomes isLongClickable() after this line but it remains not isClickable() - so I cannot see how this context menu callback can affect short click functionality.

I've solved this problem for me by adding this listener at the whole ExpandableListView and then patching the menu for children. But it looks ugly. Do I miss something?

Thanks.


I found new: in getChildView:

convertView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
                String menuItems[];
                menuItems = getResources().getStringArray(R.array.contextmenu);
                menu.setHeaderTitle(getResources().getString(R.string.sms_select));
                for (int i = 0; i<menuItems.length; i++) {
                menu.add(Menu.NONE, i, i, menuItems[i]);
                }
            }
});

and :

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
        int menuItemIndex = item.getItemId();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜