长按时不会弹出上下文菜单
长按列表视图中的列表项时不会弹出上下文菜单。我扩展了基本适配器并使用视图持有者来实现带有文本视图和图像按钮的自定义列表。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当列表视图具有复选框、单选按钮等可聚焦项目时,会出现此类问题。要在可聚焦项目的列表项布局中解决此问题,包括:
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 :
为什么不使用
ListActivity
?在我的 ListActivity 中,我有:
Why didn't you use
ListActivity
?In my ListActivity I have:
使用 ListView 的 OnItemLongClickListener (通过 set~)方法。
Use OnItemLongClickListener (via the set~) method of the ListView.