Android列表视图 - 长按一次显示错误的上下文菜单

时间:2013-11-06 12:23:55

标签: android android-listview android-contextmenu

我有两个listviews,bookmarklistview和historylistview,一个在上面 另一个。在historylistview中隐藏Bookmarklistview 显示,反之亦然。对于长时间点击事件,我正在创建一个 上下文菜单。使用historylistview可以很好地处理长按事件。

现在当我长时间点击bookmarklistview中的项目时出现问题, 显示historylistview上下文菜单而不是bookmarklistview 上下文菜单。

这怎么可能发生?有什么想法吗?

以下是bookmarklistview contextmenu的代码

public void onCreateContextMenuBookmark(ContextMenu menu, View v, ContextMenuInfo menuInfo){
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Delete Bookmark?");
    menu.add(1, 1, 1, "Delete");
    menu.add(1, 2, 2, "Cancel");
}

public boolean onContextItemSelectedBookmark(MenuItem item){
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

    if(item.getTitle() == "Delete"){
    //  delete item
    }else if(item.getTitle() == "Cancel"){
    }
    return false;
}

以下是historylistview contextmenu的代码

public void onCreateContextMenuHistory(ContextMenu menu, View v, ContextMenuInfo menuInfo){
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Delete History?");
    menu.add(1, 1, 1, "Delete");
    menu.add(1, 2, 2, "Cancel");
}

@Override
public boolean onContextItemSelected(MenuItem item){
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

    if(item.getTitle() == "Delete"){
        //     delete item
    }else if(item.getTitle() == "Cancel"){
    }
    return false;
}

好的,我发现我可以为这两个视图设置一个上下文菜单,就像这样

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)  
{
    super.onCreateContextMenu(menu, v, menuInfo);
    switch(v.getId()){
    case R.id.history_listView:
        menu.setHeaderTitle("Delete History?");
        menu.add(0, v.getId(), 0, "Delete");
        menu.add(0, v.getId(), 0, "Cancel");
        break;

    case R.id.bookmark_listView:
        menu.setHeaderTitle("Delete Bookmark?");
        menu.add(0, v.getId(), 0, "Delete");
        menu.add(0, v.getId(), 0, "Cancel");
    }
}

但是如何为每个视图调用两个不同的onContextItemSelected()方法?

1 个答案:

答案 0 :(得分:0)

你能试试这个方法吗

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    if(v.getId() == R.id.cover){

    menu.setHeaderTitle("Change Cover");
    menu.add(0, v.getId(), 0, "Take Photo");
    menu.add(0, v.getId(), 0, "Choose From Library");
    }
    else if(v.getId() == R.id.Profilepic){

    menu.setHeaderTitle("Change Profile");
    menu.add(1, v.getId(), 0, "Take Photo");
    menu.add(1, v.getId(), 0, "Choose From Library");
    }


}

@Override
public boolean onContextItemSelected(MenuItem item) {
    if (item.getGroupId() == 0) {

        if(item.getTitle()=="Take Photo"){ showCoverCameraFragment(); }
        else if(item.getTitle()=="Choose From Library"){showCoverLibraryFragment();}
        else {return false;}

    } else if (item.getGroupId() == 1) {

        if(item.getTitle()=="Take Photo"){showProfileCameraFragment();}
        else if(item.getTitle()=="Choose From Library"){showProfileLibraryFragment();}
        else {return false;}
    }




    return true;
}