onCreateonCreateContextMenu的其它參數(shù)的作用
@Override public?void?onCreateContextMenu(ContextMenu?menu,?View?view,?ContextMenu.ContextMenuInfo?menuInfo)?{ ????//設置Menu顯示內容 ????menu.setHeaderTitle("文件操作"); ????menu.setHeaderIcon(R.mipmap.ic_launcher); ????//通過讀取xml文件來設置 ????this.getMenuInflater().inflate(R.menu.menu_context,menu); ????super.onCreateContextMenu(menu,?v,?menuInfo); }
如上,onCreateContextMenu,除了menu,還有view
和menuInfo兩個參數(shù),它們怎么使用,或者它們的功能是什么?
2015-12-15
menuInfo:
API文檔的解釋是:
Additional information regarding the creation of the context menu. ?For example, ?AdapterViews use this to pass the exact item position within the adapter that initiated the context menu.
我的理解是:ListView、GridView等AdapterView利用這個參數(shù)傳遞了被點擊item的具體position或id等信息。
用處:獲取長按項的值,作為上下文菜單的標題。例如:
int position = ((AdapterContextMenuInfo)menuInfo).position;??
menu.setHeaderTitle(mDatas.get(position).title);
view:注冊該上下文的view。例如:
this.registerForContextMenu(mListView);
則此處view即為mListView。
希望我的解釋對你有用~