如何禁用Android中特定ListView项的上下文菜单

时间:2012-03-18 16:23:04

标签: android listview contextmenu

我知道之前已经问过这个问题,答复是覆盖onPrepareContextMenu()/ onCreateContextMenu()。但我不明白,也没有得到任何解决方案。请帮我看看如何禁用特定ListView项目的上下文菜单。

2 个答案:

答案 0 :(得分:5)

打开上下文菜单取决于您的逻辑。例如,在方法onItemClick(在listView中)中,您应该检查项目的内容并显示或不显示上下文菜单。我不明白,为什么它对你来说有问题?

更新

public class ExampleActivity extends ListActivity {

    private ListView mListView;
    private ArrayList<String> mList = new ArrayList<String>();
    private ArrayAdapter<String> mArrayAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mArrayAdapter = new ArrayAdapter<String>(this, android.R.id.list ,mList);
        mListView = (ListView) findViewById(android.R.id.list);
        setListAdapter(mArrayAdapter);
        registerForContextMenu(mListView);

        mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView , View v,int position, long id) {
                mListView.getItemAtPosition(position); //check current item with your logic and show or don't show contextMenu
                // for example I will show
                mListView.showContextMenu(); //to show
                return true;
            }
        });
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        // add contextmenu items
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // todo some logic...
        return super.onContextItemSelected(item);
    }

}

可能会帮助你......祝你好运......

答案 1 :(得分:1)

此外,您可以重新注册列表视图,这将导致您的onCreateContextMenu方法再次执行

ListView lv = (ListView) findViewById(R.id.lvExample);
registerForContextMenu(lv);

然后你可以在onCreateContextMenu方法中隐藏或显示你想要的逻辑菜单项