0

根据Android文档registerForContextMenu可以多次调用不同的视图:

为给定视图注册要显示的上下文菜单(多个视图可以显示上下文菜单)。

我尝试将此功能用于以下布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/host"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:longClickable="true" >
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

在源代码registerForContextMenu中,我同时调用主机和列表。不幸的是,这样的注册会导致一个问题:在onContextItemSelected(MenuItem item)我总是null通过item.getMenuInfo(). 当然,在这个用例中,长按是在列表视图项上进行的,因此菜单项通常不应为空。如果我删除registerForContextMenu主机布局,则菜单信息会在onContextItemSelected.

我需要上下文菜单的多次注册,以确保在屏幕上的任何位置显示它以进行触摸(可以在另一个问题中找到详细信息 -此处)。简而言之,上下文菜单应该出现在项目和任何项目之外的触摸上,如果为项目调用它,我需要获取项目位置。

4

1 回答 1

1

这不是因为您使用了多个 registerForContextMenu,而是因为您在包含另一个的视图上设置了一个。所以它们都注册为来自主机而不是列表。

常规视图不会有额外的菜单信息。

尝试关闭主机的焦点,让它进入列表。它也应该仍然适用于主机,它只是不会在列表之前抓住焦点。

于 2012-05-06T18:38:00.177 回答