为RecycleView添加ContextMenu支持
来源:互联网 发布:mac 股票行情软件 编辑:程序博客网 时间:2024/06/05 15:56
最近实现了如下的长按删除功能,运用了上下文菜单,在ListView和GrideView上运用正常,但是在RecyclerView上就不行了(可以弹出上下文菜单,但是点击菜单item会报空指针异常:MenuInfo为null),上网一顿搜索,才知道RecyclerView没有实现对ContextMenu的支持,那只好自己去实现了。
首先,创建一个类继承RecyclerView并去实现支持上下文菜单的方法:
1.publicAdapterView.AdapterContextMenuInfogetContextMenuInfo()
2.publicbooleanshowContextMenuForChild(View originalView)
/** * Description: 实现内容菜单支持的RecyclerView * Created by Z on 2017/9/14. */public class RecyclerViewImplementsContextMenu extends RecyclerView { private AdapterView.AdapterContextMenuInfo contextMenuInfo; public RecyclerViewImplementsContextMenu(Context context) { super(context); } public RecyclerViewImplementsContextMenu(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public RecyclerViewImplementsContextMenu(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public AdapterView.AdapterContextMenuInfo getContextMenuInfo() { return contextMenuInfo; } @Override public boolean showContextMenuForChild(View originalView) { int position = getChildAdapterPosition(originalView); long longId = getChildItemId(originalView); contextMenuInfo = new AdapterView.AdapterContextMenuInfo(originalView,position,longId); return super.showContextMenuForChild(originalView); }}
然后,就没有然后啦,直接用就可以了(哈哈哈哈哈,嗝...)
在用到的地方去实现这两个方法就行了:
@Overridepublic void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.menu_delecte_content,menu);}@Overridepublic boolean onContextItemSelected(MenuItem item) { doSomething(); return super.onContextIteSelected(item);}
当然,千万千万不要忘记去注册咱们的ContextMenu
registerForContextMenu(View view);
最后,感谢网上大佬们的分享,贴个参考链接: 此处是参考原文
阅读全文
0 0
- 为RecycleView添加ContextMenu支持
- 为RecyclerView添加ContextMenu
- 为工程添加上下文菜单(ContextMenu)
- 简洁大方,为自己的Android App添加一个ContextMenu
- 支持上下拉刷新的RecycleView,基于androidPullToRefresh,添加RecycleView HeaderView
- 为ListView创建ContextMenu
- 为ListView创建ContextMenu
- 为应用程序添加脚本支持
- 为应用程序添加脚本支持
- 为DWR添加CDI支持
- 为qxmpp添加 AdHocCommand 支持
- 为UIAlertView添加block支持
- 为UIAlertView添加block支持
- 为Vim 添加vimgdb支持
- 为freeswitch 添加h323支持
- 打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(一)基础篇
- 打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(三)具体实现
- RecycleView添加分割线。
- vortex-报错集
- 用swagger-ui展示不同路径多个项目的接口文档
- RHEL7 -- 使用Chrony设置时间与时钟服务器同步
- RStudio Server-0.99.902 (OpenLogic CentOS 7.2)
- The 10 most popular coding challenge websites for 2017
- 为RecycleView添加ContextMenu支持
- Python dictionary 汇总
- Docker技术剖析--docker数据卷
- mysql的binlog导出为可识别的sql语句
- SSM框架整合(Maven+Spring+SpringMVC+Mybatis+mysql 附demo实例)
- MySQL 存储过程和存储函数示例
- 高德地图的坑
- jmeter源码---gui简单介绍
- 用dom4j解析xml,soap报文