Android Long Press Spinner View

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 02:46:54

问题


I want to be able to long press items in the spinner view and have a contextMenu appear. I tried this code:

spinner = (Spinner) findViewById(R.id.catagorySpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,android.R.layout.simple_dropdown_item_1line, data);
spinner.setAdapter(adapter);
registerForContextMenu(spinner);

but as you can guess this added a context menu to the actual Spinner and not to the contents inside. Does anyone know how i can do this?


回答1:


Have you tried this?

        spinner.setOnItemLongClickListener(new OnItemLongClickListener() {

            public boolean onItemLongClick(AdapterView<?> parent, View view, int arg2, long arg3) {

                view.showContextMenu();
                return true;

            }
        });



回答2:


You can register for each item inside the adapter's getView() method.

View getView(View convertView, ... ) {
   ....
   // inflate view or reuse.
   ....
   getContext().registerForContextMenu(convertView);
   ....
   return convertView;
}


来源:https://stackoverflow.com/questions/7462934/android-long-press-spinner-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!