基于所选列表视图项的一个活动中的多个上下文菜单

时间:2015-02-09 23:41:29

标签: java android android-intent android-activity android-studio

如何根据选择的列表视图项以及上下文菜单中基于选择的上下文菜单项的唯一if语句打开不同的上下文菜单?

下面的代码适用于按钮但是如何才能对列表视图项目进行操作+ +我上面要求的内容?

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        if(v.getId() == R.id.first_button)
             inflate one menu
        else if(v.getId() == R.id.second_button)
             inflate another menu        
}

1 个答案:

答案 0 :(得分:0)

第一种方式:ContextMenu.ContextMenuInfo有关于应该显示上下文菜单的项目的额外信息。此信息将根据v的类别而有所不同,因此您可以执行此操作,复制并粘贴此

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo info) {
    super.onCreateContextMenu(menu, v, info);        
    int index = info.position; //The position in the adapter for which the context menu is being displayed. 
    View child = info.targetView;//The child view for which the context menu is being displayed. 
    // so for your case you have to use the child,because of your way about it
    if(child.getId() == R.id.first_button)
         inflate one menu
    else if(child.getId() == R.id.second_button)
         inflate another menu        
}

让我知道它是否有用