[英]ListView in PopupWindow,ContextMenu not popping up on Long click
[英]ContextMenu not popping up on Long click
長按單擊列表視圖中的列表項時,不會彈出上下文菜單。 我已經擴展了基本適配器並使用了視圖持有者來實現帶有textviews和圖像按鈕的自定義列表。
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"
/>
我已經嘗試了很多天了。 我認為不可能為這樣的自定義列表視圖注冊Context-menu。 如果我錯了,請糾正我(可能帶有示例代碼)。
現在我正在考慮向列表項添加一個按鈕,它會在單擊它時顯示一個菜單。 是否可以通過其他方式使用Dialogs?
任何幫助將非常感激..
當列表視圖具有復選框,radioButton等可聚焦項時,會出現這樣的問題。 要在可聚焦項目的列表項的布局中解決此問題,請包括:
android:focusable="false";
你為什么不使用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;
}
使用ListView的OnItemLongClickListener(通過set~)方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.