问题
I have an Intent that extends a ListActivity. In my onCreate
method after having populated the list adapter I use registerForContextMenu(getListView());
to register for a context menu.
Now it is working and the context menu has its original function which is; once I click and hold down on an item the context menu opens.
Can I open the context menu on a single click (without having to hold down on the list)?
All help is appreciated.
回答1:
call activity.openContextMenu(l)
onitem click event to open contextmenu on single click and onLongClick call activity.closeContextMenu()
Example
import android.app.Activity;
import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MyListView extends ListActivity implements OnItemLongClickListener {
/** Called when the activity is first created. */
Activity activity = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, PENS);
setListAdapter(arrayAdapter);
getListView().setTextFilterEnabled(true);
ListView lv = getListView();
this.registerForContextMenu(lv);
lv.setOnItemLongClickListener(this);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
System.out.println("...11configuration is changed...");
}
static final String[] PENS = new String[]{
"MONT Blanc",
"Gucci",
"Parker",
"Sailor",
"Porsche Design",
"item1",
"item2",
"item3",
"item4",
"item5",
"item6",
"item7",
"item8",
"item9",
"item10",
"item11"
};
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
activity.openContextMenu(l);
System.out.println("...context is called");
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
System.out.println("...on create context menu...");
super.onCreateContextMenu(menu, v, menuInfo);
}
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
System.out.println("...on long click close context menu...");
activity.closeContextMenu();
// TODO Auto-generated method stub
return false;
}
回答2:
Here is another simpler way to show context menu on single click.
private void addOnClickListener()
{
contactList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
view.showContextMenu();
}
})
}
replace contactList by your ListView
and make sure to call this method after the initialization of ListView
.
回答3:
I don't think it's working smoothly. Calling openContextMenu(l)
will cause item.getMenuInfo()
to be null (inside method onContextItemSelected(MenuItem item)
).
You should call l.showContextMenuForChild(v)
instead of openContextMenu(l)
.
回答4:
this work perfect....
listmp3 = (ListView) findViewById(R.id.results_mp3);
registerForContextMenu(listmp3);
listmp3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listmp3.showContextMenuForChild(view);
}
});
来源:https://stackoverflow.com/questions/6435073/android-context-menu-on-single-click