简体   繁体   English

ContextMenu没有弹出长按

[英]ContextMenu not popping up on Long click

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. 我已经扩展了基本适配器并使用了视图持有者来实现带有textviews和图像按钮的自定义列表。

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

Implementation of onCreateContextMenu 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);
 }

The XML for listview is here listview的XML就在这里

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

I've been trying this for many days. 我已经尝试了很多天了。 I think its impossible to register Context-menu for a custom list view like this. 我认为不可能为这样的自定义列表视图注册Context-menu。 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? 是否可以通过其他方式使用Dialogs?

Any help would be much appreciated.. 任何帮助将非常感激..

Such problem arises in list view when it has focusable items like checkbox, radioButton,etc. 当列表视图具有复选框,radioButton等可聚焦项时,会出现这样的问题。 To solve this in the layout for the list item for the focusable items include : 要在可聚焦项目的列表项的布局中解决此问题,请包括:

android:focusable="false";

Why didn't you use ListActivity ? 你为什么不使用ListActivity

In my ListActivity I have: 在我的ListActivity中,我有:

@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;
    }

使用ListView的OnItemLongClickListener(通过set~)方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM