当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Do not show context menu for first listItem in the ListView

问题描述:

I have a listview which has some listItems in it. I have registered the listview for context menu using registerForContextMenu(mListView);. Now what I want is that, if the user long presses on the first item of the listview, the context menu for that item should not be shown, but for all the rest items, it should popup the context menus if the user long presses on them. Can we do this?

网友答案:

Instead, i would suggest you to use OnItemLongClickListener which will return position value of long-pressed item. By considering that value, you may decide either to show dialog with list of options or just ignore it.

For example:

yourListView.setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
            int position, long id) {
        if(position != 0){  //ignoring first item from list
            //do whatever you want
        }
        return false;
    }
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: