是否可以在列表视图中的项目位置显示上下文菜单?

时间:2015-09-30 14:46:19

标签: android listview android-listview android-contextmenu

我有一个列表视图。我想单击一个项目并在单击项目位置显示上下文菜单。目前,我使用以下代码显示上下文菜单,并在单击该项目时执行一些操作。它工作正常,但上下文菜单位置是我屏幕中心的自动设置。我该如何解决?

public class ManageActivity extends Activity {
  private ListView mainListView ;
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_main);    
    // Find the ListView resource. 
    mainListView = (ListView) findViewById( R.id.mainListView );
    mainListView.setAdapter( listAdapter );     
    registerForContextMenu(mainListView);
 }
  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
      menu.add(0, v.getId(), 0, "Edit");
      menu.add(0, v.getId(), 0, "Delete");
  }
  @Override
  public boolean onContextItemSelected(MenuItem item){
     AdapterView.AdapterContextMenuInfo acm = (AdapterContextMenuInfo) item.getMenuInfo();
     String  audio_file_name = (String) mainListView.getItemAtPosition(acm.position);

     if(item.getTitle()=="Edit")
        Toast.makeText(getApplicationContext(), "Edit Clicked", Toast.LENGTH_LONG).show();
     if(item.getTitle()=="Delete")
          Toast.makeText(getApplicationContext(), "Delete Clicked", Toast.LENGTH_LONG).show();
    return true;
}

2 个答案:

答案 0 :(得分:3)

答案是肯定的。一个完美的例子是Google Play App中的gridview项目。基本上,您可以在此处找到更多详细信息:

Android - Popup menu when list item view pressed?

答案 1 :(得分:1)

ListView.setonItemLongClickListener();然后你将popumenu锚定到它