我已经添加了
列表视图中的长按registerForContextMenu(this.objListView);
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
System.out.println("ENTERED IN THE CONTEXT MENU BLOCK");
if (v.getId() == R.id.booksLV) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
String objBook = userVector.get(info.position);
menu.setHeaderTitle(objBook);
String[] menuItems = {"Edit","Delete" };
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
<ListView
android:id="@+id/booksLV"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="0.5"
android:dividerHeight="1px"
android:paddingRight="1dip"
>
</ListView>
自定义适配器组件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listitem_test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/nameLL"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/pure_white_color"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:padding="10dip" >
<TextView
android:id="@+id/nameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/TextView01"
android:textAppearance="?android:attr/textAppearanceSmall">
</TextView>
</LinearLayout>
<LinearLayout
android:id="@+id/headingLL"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/off_white_color" >
<TextView
android:id="@+id/headingTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dip"
android:text="@+id/TextView01"
android:textColor="@color/darkGray"
android:textAppearance="?android:attr/textAppearanceSmall">
</TextView>
</LinearLayout>
</LinearLayout>
oncreate
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.savedInstanceState = savedInstanceState;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
System.out.println(width + " ++++++++++++++++++++ " + height);
Toast.makeText(this, width + " " + height, Toast.LENGTH_LONG).show();
if (width == 240 && height == 400) {
setContentView(R.layout.activity_flat_accountlist_exp_h);
} else if (width == 480 && height == 856) {
setContentView(R.layout.activity_flat_accountlist_exp_h);
} else if (width == 480 && height == 854) {
setContentView(R.layout.activity_flat_accountlist_exp_h);
} else if (width == 600 && height == 976) {
setContentView(R.layout.activity_flat_accountlist_exp_mdpi_h);
} else if (width == 600 && height == 1024) {
setContentView(R.layout.activity_flat_accountlist_exp_mdpi_h);
} else {
setContentView(R.layout.activity_flat_accountlist);
}
float scale = this.getResources().getDisplayMetrics().density;
mSearchView = (SearchView) findViewById(R.id.searchView);
objListView = (ListView) findViewById(R.id.booksLV);
registerForContextMenu(objListView);
selectedIndex = (TextView) findViewById(R.id.selectedIndex);
showProcessDialog();
getAccounts();
}
长按
时不会显示上下文菜单请帮帮我
由于
答案 0 :(得分:2)
在Listview上尝试下面的上下文菜单代码..
首先在listview上注册您的内容菜单..
ListView mListView = (ListView)findViewById(R.id.listview);
registerForContextMenu(mListView);
在内容菜单上添加菜单...
@Override
public void onCreateContextMenu(ContextMenu menu,
View v, ContextMenuInfo menuInfo) {
menu.add(0, 1, 0, "Add");
menu.add(0, 2, 1, "Rename");
menu.add(0, 3, 2, "Delete");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
super.onContextItemSelected(item);
if(item.getTitle().equals("Add")) {
//Add code
} else if(item.getTitle().equals("Rename")) {
//Rename code
} else if(item.getTitle().equals("Delete")) {
//Delete code
}
return true;
};
答案 1 :(得分:0)
您必须在onCreate方法
中向视图添加longclicklistenerthis.objListView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// Here open the context menu
openContextMenu(this.objListView);
}
});
我希望它有所帮助
答案 2 :(得分:0)
你的onCreateContextMenu()方法是否被调用?
如果是,您是否在添加项目后尝试调用超级:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
System.out.println("ENTERED IN THE CONTEXT MENU BLOCK");
if (v.getId() == R.id.booksLV)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
String objBook = userVector.get(info.position);
menu.setHeaderTitle(objBook);
String[] menuItems = {"Edit","Delete" };
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
super.onCreateContextMenu(menu, v, menuInfo);
}