问题
I want to be able to long press items in the spinner view and have a contextMenu appear. I tried this code:
spinner = (Spinner) findViewById(R.id.catagorySpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,android.R.layout.simple_dropdown_item_1line, data);
spinner.setAdapter(adapter);
registerForContextMenu(spinner);
but as you can guess this added a context menu to the actual Spinner and not to the contents inside. Does anyone know how i can do this?
回答1:
Have you tried this?
spinner.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int arg2, long arg3) {
view.showContextMenu();
return true;
}
});
回答2:
You can register for each item inside the adapter's getView()
method.
View getView(View convertView, ... ) {
....
// inflate view or reuse.
....
getContext().registerForContextMenu(convertView);
....
return convertView;
}
来源:https://stackoverflow.com/questions/7462934/android-long-press-spinner-view