android fragment中使用ContextMenu
2015-07-31 12:47
375 查看
在fragment中使用ContextMenu和在Activity中使用的步骤是一样的。
1.在layout--》menu文件夹下的.xml文件中定义菜单选项。
2.在你的fragment中重写onCreateContextMenu
这个listView是你的点击的长时间点击出现contextMeau的view,可能是一个ListView,或者是GridView.
这样就可以了,看效果图
1.在layout--》menu文件夹下的.xml文件中定义菜单选项。
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.viewpager_fragment.MainActivity" > <item android:id="@+id/id_delete" android:orderInCategory="100" android:showAsAction="never" android:title="delete"/> <item android:id="@+id/id_share" android:orderInCategory="101" android:showAsAction="never" android:title="share"/> </menu>
2.在你的fragment中重写onCreateContextMenu
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.main, menu); }3.重写onContextItemSelected方法,我这里就用一个吐司示意了。
@Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.id_delete: Toast.makeText(getActivity(), "you selected delete", Toast.LENGTH_SHORT).show(); break; default: break; } return super.onContextItemSelected(item); }4.千万不要忘了在fragment中的oncreateView方法中添加registerForContextMenu(listView);
这个listView是你的点击的长时间点击出现contextMeau的view,可能是一个ListView,或者是GridView.
这样就可以了,看效果图
相关文章推荐
- Android imageView图片按比例缩放
- 号称最快android虚拟机的genymotion安装出现virtualization engine not found plugin loading aborted另外一个原因
- android:匿名启动系统Activity方式(系统浏览器、相册、短信、电话)
- Android:异步消息源码解析
- Android之常用类型转换
- Android ListView相关功能介绍
- android 实现progressdialog 等待界面
- android.content.res.Resources$NotFoundException:+String+resource+ID+#0x1
- android 定制自己的log工具类
- Android 的manifest解析
- 从零开始学习android,记录他人的博客
- android-友盟第三方登录,及分享的使用
- 说说Android应用的persistent属性
- Android NDK *** could not be resolved
- android studion Gradle多渠道打包
- 图解Android应用程序构建原理
- Android开发笔记---service的简单使用
- Android-倒计时工具类
- Android端与Java服务端交互——SocketIO
- 解读Android虚拟机工作原理