长按时不会弹出上下文菜单

发布于 2024-09-01 18:25:15 字数 985 浏览 0 评论 0原文

长按列表视图中的列表项时不会弹出上下文菜单。我扩展了基本适配器并使用视图持有者来实现带有文本视图和图像按钮的自定义列表。

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

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

listview 的 XML 在这里

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

我已经尝试了很多天了。我认为不可能为这样的自定义列表视图注册上下文菜单。如果我错了,请纠正我(可能使用示例代码)。

现在我正在考虑向列表项添加一个按钮,单击它时会显示一个菜单。除了使用对话框之外,是否可以使用其他方式?

任何帮助将不胜感激..

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);  

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

The XML for listview is here

 <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. 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?

Any help would be much appreciated..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哀由 2024-09-08 18:25:15

当列表视图具有复选框、单选按钮等可聚焦项目时,会出现此类问题。要在可聚焦项目的列表项布局中解决此问题,包括:

android:focusable="false";

Such problem arises in list view when it has focusable items like checkbox, radioButton,etc. To solve this in the layout for the list item for the focusable items include :

android:focusable="false";
江城子 2024-09-08 18:25:15

为什么不使用ListActivity

在我的 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;
    }

Why didn't you use 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;
    }
关于从前 2024-09-08 18:25:15

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

Use OnItemLongClickListener (via the set~) method of the ListView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文