Android列表视图:更改上下文菜单项取决于单击的项目

时间:2023-01-16 12:29:26

I need to change list of menu items(actually hide one or just make it not clickable) depending on list item long-clicks. I asume I can do it somehow in setOnItemLongClickListener but can't find solution:

我需要更改菜单项列表(实际上隐藏一个或只是让它不可点击),具体取决于列表项长按。我认为我可以在setOnItemLongClickListener中以某种方式做到但无法找到解决方案:

public void onCreate(Bundle savedInstanceState){
......
registerForContextMenu(listView);
listView.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView , View v,int position, long id) {             
            MyListItem item = (MyListItem) listView.getItemAtPosition(position);
            listView.showContextMenu(); 
            return true;
        }
    });

1 个解决方案

#1


0  

Just found only solution with global variable to set value in OnItemLongClickListener depend on item clicked values and check it in onCreateContextMenu. Also can save there just clicked item (MyListItem). But I think exists more elegant solution.

刚发现只有具有全局变量的解决方案才能在OnItemLongClickListener中设置值取决于项目点击的值并在onCreateContextMenu中进行检查。也可以保存那里只是点击项目(MyListItem)。但我认为存在更优雅的解决方案。

public void onCreateContextMenu(ContextMenu menu, View v,   ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if(!someGlobalVar){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.list_item, menu);           
    }
    ...
    or
    if(!someGlobalVar){ 
       menu.getItem(0).setVisible(someGlobalVar);
    }
}

#1


0  

Just found only solution with global variable to set value in OnItemLongClickListener depend on item clicked values and check it in onCreateContextMenu. Also can save there just clicked item (MyListItem). But I think exists more elegant solution.

刚发现只有具有全局变量的解决方案才能在OnItemLongClickListener中设置值取决于项目点击的值并在onCreateContextMenu中进行检查。也可以保存那里只是点击项目(MyListItem)。但我认为存在更优雅的解决方案。

public void onCreateContextMenu(ContextMenu menu, View v,   ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if(!someGlobalVar){
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.list_item, menu);           
    }
    ...
    or
    if(!someGlobalVar){ 
       menu.getItem(0).setVisible(someGlobalVar);
    }
}