文本菜单不是长按弹出 [英] ContextMenu not popping up on Long click

查看:108
本文介绍了文本菜单不是长按弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文菜单没有出现在北京的长期点击列表视图列表项。我已经扩展底座适配器和使用的视图持有人实现自定义列表textviews和ImageButton的。

The context menu is not popping up on the long click on the list items in the list view. I've extended the base adapter and used a view holder to implement the custom list with textviews and an imagebutton.

adapter = new MyClickableListAdapter(this, R.layout.timeline, mObjectList);
       list.setAdapter(adapter);
       registerForContextMenu(list);  

onCreateContextMenu实施

Implementation of onCreateContextMenu

  @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  // TODO Auto-generated method stub
  super.onCreateContextMenu(menu, v, menuInfo);

  Log.d(TAG, "Entering Context Menu");

   menu.setHeaderTitle("Context Menu");

  menu.add(Menu.NONE, DELETE_ID, Menu.NONE, "Delete")
  .setIcon(R.drawable.icon);
 }

中的XML为列表视图是在这里

The XML for listview is here

 <ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

我一直想这个了很多天。我认为它不可能注册上下文菜单这样的自定义列表视图。纠正我,如果我错了(可能与样本code)。

I've been trying this for many days. I think its impossible to register Context-menu for a custom list view like this. Correct me if I am wrong (possibly with sample code).

现在我想到的添加按钮,列表项,并将其显示在菜单上单击它。是否有可能比使用对话框一些其他的方式?

Now I am thinking of a adding a button to the list item and it displays a menu on clicking it. Is it possible with some other way than using Dialogs?

任何帮助将是非常美联社preciated ..

Any help would be much appreciated..

推荐答案

你为什么不使用 ListActivity

在我ListActivity我有:

In my ListActivity I have:

@Override
protected void onCreate(Bundle savedInstanceState) {
    /* setContentView() and all stuff that happens in this method */
    registerForContextMenu(getListView());
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterView.AdapterContextMenuInfo info;
    try {
        info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    } catch (ClassCastException e) {
        Log.e(TAG, "bad menuInfo", e);
    return;
    }

    Something something = (Subway) getListAdapter().getItem(info.position);
    menu.setHeaderTitle(something.getName());
    menu.setHeaderIcon(something.getIcon());
    menu.add(0, CONTEXT_MENU_SHARE, 0, "Do something!");
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    AdapterView.AdapterContextMenuInfo info;
    try {
        info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    } catch (ClassCastException e) {
        Log.e(TAG, "bad menuInfo", e);
        return false;
    }

    switch (item.getItemId()) {
        case DO_SOMETHING:
            /* Do sothing with the id */
            Something something = getListAdapter().getItem(info.position);
            return true;
    }

这篇关于文本菜单不是长按弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆