首先,我的英语不太好。
我有一个 ListView
like this另一个带有 TextView
。
我的问题是我想在那里放一个 contextMenu
,但我不能。我为此浪费了很多时间,找不到解决方案。
我使用了 registerForContextMenu(listViewTotes)
,以及 onCreateContextMenu
和 onContextItemSelected
。
谢谢!
最佳答案
我建议您使用 OnItemLongClickListener()。看起来,OnItemClickListener() 没有响应 registerForContextMenu(arg0)。
以使用OnItemLongClickListener()为例,代码如下:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.my_list);
adapter = new MyAdapter(this,getModel());
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new PlayListOnItemLongClickListener());
}
private class PlayListOnItemLongClickListener implements AdapterView.OnItemLongClickListener {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
registerForContextMenu(arg0);
return false;
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.context_playlist_operation, menu);
menu.setHeaderIcon(R.drawable.ic_launcher);
menu.setHeaderTitle("What do you want to do");
}
在/ListViewTest/res/menu/context_playlist_operation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<group>
<item
android:id="@+id/context_playlist_remove_playlist"
android:title="@string/app_name"
/>
</group>
</menu>
关于android - 带有 ContextMenu 选项的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12946088/