ExpandableListView及其子项显示上下文菜单
来源:互联网 发布:手机音频编辑软件中文 编辑:程序博客网 时间:2024/06/06 01:44
1.编写上下文菜单的xml
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/deletehistorybutton" android:title="删除历史记录"> </item><item android:id="@+id/deletehistorybutton2" android:title="删除历史记录2"> </item> </menu>
2.生成上下文菜单
@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {// TODO Auto-generated method stubsuper.onCreateContextMenu(menu, v, menuInfo);menu.setHeaderTitle("Sample menu");//显示 选中项目的group child 即使选中子项也是-1 group正常ExpandableListView.ExpandableListContextMenuInfo info= (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;int type = ExpandableListView.getPackedPositionType(info.packedPosition); int group = ExpandableListView.getPackedPositionGroup(info.packedPosition); int child = ExpandableListView.getPackedPositionChild(info.packedPosition); Log.d("onCreateContextMenu","type:"+type+"group:"+group+"child:"+child); getMenuInflater().inflate(R.menu.options, menu);////绑定菜单xml}
3.响应上下文菜单
@Overridepublic boolean onContextItemSelected(MenuItem item) {// TODO Auto-generated method stubsuper.onContextItemSelected(item);switch(item.getItemId()){case R.id.deletehistorybutton:Toast.makeText(mContext, "delete",Toast.LENGTH_SHORT).show();break;case R.id.deletehistorybutton2:Toast.makeText(mContext, "delete2",Toast.LENGTH_SHORT).show();break;default:break;}return super.onContextItemSelected(item);}
4.长按监听并显示菜单
expandableListView.setOnItemLongClickListener(new OnItemLongClickListener(){@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View childView,int flatPos, long id) {// TODO Auto-generated method stubLog.d("LongClick", "start"); if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD){long packedPos = ((ExpandableListView)parent).getExpandableListPosition(flatPos);int groupPosition = ExpandableListView.getPackedPositionGroup(packedPos);int childPosition = ExpandableListView.getPackedPositionChild(packedPos); Log.d("LongClick", "get position"); expandableListView.showContextMenu(); ///////子项显示菜单 Log.d("LongClick","groupPosition:"+groupPosition+"childPosition:"+childPosition);return true;}return false;}});
5注册菜单
registerForContextMenu(expandableListView);
0 0
- ExpandableListView及其子项显示上下文菜单
- ExpandableListView子项无法弹出上下文菜单的解决办法
- ExpandableListView上下文菜单的实现
- ExpandableListView的使用及注册上下文菜单
- 选项菜单+上下文菜单+子菜单
- 选项菜单+上下文菜单+子菜单
- Android的上下文菜单和子菜单
- ExpandableListView的长按点击弹出上下文菜单方法
- ExpandableListView主菜单跟子菜单
- 选项菜单、子菜单、上下文菜单的使用
- 【Android基础知识】选项菜单、上下文菜单、子菜单的使用
- Android基础知识】选项菜单、上下文菜单、子菜单的使用
- 脚本显示子菜单
- 对上下文菜单和PopopMenu 以及在ActionBar中显示菜单项的总结
- Qt学习—qt上下文菜单显示
- ui.选项菜单及其子菜单
- expandablelistview的子控件及其中控件的点击事件
- 安卓菜单的使用系列3:显示上下文菜单
- JBPM视频下载链接
- Hibernate报表查询
- iOS TableView didSelectRowAtIndexPath选中某Cell后获取其Cell实例对象
- Python的包管理工具easy_install, setuptools, pip,distribute介绍
- linux tcp多线程服务器与客户端编程实例
- ExpandableListView及其子项显示上下文菜单
- 关于_load_gdtr
- poj.3259
- 《黑马程序员》 交通灯管理系统案例分析与总结
- cocos2d-x 2.2版本下实现随机姓名产生
- Linux系统上安装MySQL 5.5prm
- js金额格式化
- After Adding a Product Redirect to Shopping Cart doesnot work
- wince按键中断的检测