##背景
我们经常在列表的页面中,点击列表中的行,一般进入详情页面,长按列表中一行,会弹出一个菜单,包含了对某一行的操作(编辑、删除等等),也知道通常的用法:

  • 0x01. 在Activity中注册需要上下文菜单的View:
    registerForContextMenu(mListView);
  • 0x02. 然后在Activity中继承onCreateContextMenu方法,添加菜单项:

    1
    2
    3
    4
    5
    6
    7
    8
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    Log.d(LOG_TAG, "onCreateContextMenu");
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle(R.string.prompt);
    menu.add(Menu.NONE, R.id.context_menu_item_delete_record, Menu.NONE, R.string.delete_record);//groupId, itemId, order, title
    menu.add(Menu.NONE, R.id.context_menu_item_delete_record_with_file, Menu.NONE, R.string.delete_record_with_file);
    }

    PS:每次长按出现上下文菜单都会调用这个方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    /** * Called when a context menu for the {@code view} is about to
    be shown. * Unlike {@link #onCreateOptionsMenu(Menu)}, this will
    be called every * time the context menu is about to be shown and
    should be populated for * the view (or item inside the view for {@link
    AdapterView} subclasses, * this can be found in the {@code
    menuInfo})). * <p> * Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
    * item has been selected. * <p> * It is not safe to hold onto the
    context menu after this method returns. * */
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    }
  • 0x03. 接下来长按列表中一行的时候,会弹出上下文菜单:

  • 0x04. 点击菜单后,在Activity中继承onContextItemSelected方法进行处理:

    1
    2
    3
    4
    5
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()){
    }
    }
  • 0x05. 获取Item标识(id)
    我们删除数据库或者一行记录的时候,要知道主键(一般是id)才能进行操作,很多人就想办法,有的是把ListView的每个ItemView添加一个LongClickListener,然后长按的时候记录下Position,然后在进行相应处理。

    其实有更优雅的做法,onContextItemSelected(MenuItem item)回调的参数item可以获取item.getMenuInfo(),在ListView和Adapter的模式中,可以强制转换成AdapterContextMenuInfo,拿到targetView(即所长按行的ItemVew,如果我们需要什么参数,直接放到View.setTag中去即可):

    1
    2
    3
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    int index = info.position;
    View view = info.targetView;

至此,常见的用法就完了,那么遇到其他自定义View呢?

  • 0x06. 自定义View的ContextMenu实现
    下面以用到的RecycleView为例,没有了ListView及其Adapter的封装,我们需要自己处理ContextMenu。
    最重要的是继承View的两个方法:
    1.上下文菜单Item的附加信息(上面item.getMenuInfo());
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    /** * Views should implement this if they have extra information to
    associate * with the context menu. The return result is supplied as a
    parameter to * the {@link
    OnCreateContextMenuListener#onCreateContextMenu(ContextMenu,
    View, ContextMenuInfo)} * callback. * * @return Extra information
    about the item for which the context menu * should be shown.
    This information will vary across different * subclasses of View. */
    protected ContextMenuInfo getContextMenuInfo() {
    return null;
    }

2.ViewGroup的showContextMenuForChild,每次弹出上下文菜单都会调用此方法,需要在这里更新ContextMenuInfo;

1
2
3
4
/** * {@inheritDoc} */
public boolean showContextMenuForChild(View originalView) {
return mParent != null && mParent.showContextMenuForChild(originalView);
}
  • 0x07. 自定义RecycleView的ContextMenu全部代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

大专栏  ConxtMenu高级用法v class="line">163

ConxtMenu高级用法的更多相关文章

  1. Visual Studio 宏的高级用法

    因为自 Visual Studio 2012 开始,微软已经取消了对宏的支持,所以本篇文章所述内容只适用于 Visual Studio 2010 或更早期版本的 VS. 在上一篇中,我已经介绍了如何编 ...

  2. SolrNet高级用法(分页、Facet查询、任意分组)

    前言 如果你在系统中用到了Solr的话,那么肯定会碰到从Solr中反推数据的需求,基于数据库数据生产索引后,那么Solr索引的数据相对准确,在电商需求中经常会碰到菜单.导航分类(比如电脑.PC的话会有 ...

  3. sqlalchemy(二)高级用法

    sqlalchemy(二)高级用法 本文将介绍sqlalchemy的高级用法. 外键以及relationship 首先创建数据库,在这里一个user对应多个address,因此需要在address上增 ...

  4. Solr学习总结(六)SolrNet的高级用法(复杂查询,分页,高亮,Facet查询)

    上一篇,讲到了SolrNet的基本用法及CURD,这个算是SolrNet 的入门知识介绍吧,昨天写完之后,有朋友评论说,这些感觉都被写烂了.没错,这些基本的用法,在网上百度,资料肯定一大堆,有一些写的 ...

  5. 再谈Newtonsoft.Json高级用法

    上一篇Newtonsoft.Json高级用法发布以后收到挺多回复的,本篇将分享几点挺有用的知识点和最近项目中用到的一个新点进行说明,做为对上篇文章的补充. 阅读目录 动态改变属性序列化名称 枚举值序列 ...

  6. Jquery remove 高级用法

    Jquery remove 高级用法 html 代码 <div class="file-image">abc1111</div><div class= ...

  7. Newtonsoft.Json高级用法(转)

    手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...

  8. redis(二)高级用法

    redis(二)高级用法 事务 redis的事务是一组命令的集合.事务同命令一样都是redis的最小执行单元,一个事务中的命令要么执行要么都不执行. 首先需要multi命令来开始事务,用exec命令来 ...

  9. 【转】 Newtonsoft.Json高级用法

    手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...

随机推荐

  1. ZJNU 1699 - Bits

    可得应当优先寻找最大的2^n-1这个数 如果l的位数不等于r的位数,那么这个数 2^n-1 就是最优解(每一位全为1) 如果l和r的位数相同,先看r是否符合 2^n-1,符合直接返回,不符合的话拆除最 ...

  2. mysql超大sql导入(10G)

    mysql  大数据库文件上传(10G) phpstudy2017 环境  mysql 5.5.53   php 5.5.45 更改php.ini memory_limit 为 2048M php.i ...

  3. Django框架(六):模型(二) 字段查询、查询集

    1. 字段查询 通过模型类.objects属性可以调用如下函数,实现对模型类对应的数据表的查询. 函数名 功能 返回值 说明 get 返回表中满足条件的一条且只能有一条数据. 返回值是一个模型类对象. ...

  4. 解决IntelliJ IDEA Community 社区版 启动Tomcat插件 "Smart Tomcat" NullPointerException 空指针异常

    IntelliJ IDEA Community社区版默认是没有Ultimate版的Tomcat Server,这时候就可以使用插件"Smart Tomcat"; 在"Ru ...

  5. jedis异常Broken pipe (Write failed)

    异常:java.net.SocketException: Broken pipe (Write failed); nested exception is redis.clients.jedis.exc ...

  6. 阿里云 asp.net core nginx 单机部署

    1. dotnet core 安装 https://www.microsoft.com/net/download#core 安装之前要安装依赖:yum install libunwind libicu ...

  7. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:读取MNIST手写图片数据写入的TFRecord文件

    import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_dat ...

  8. git clone 拉取github上面的代码报错:fatal: Authentication failed for xxx解决

    1.打开git bash,输入密码:git config --system --unset credential.helper2.结果报错:error: could not lock config f ...

  9. tcp和udp的socket形式

    Sockets编程有三种: (1).流套接字(SOCK_STREAM): (2).数据包套接字(SOCK_DGRAM): (3).原始套接字(SOCK_RAW): TCP是流套接字 UCP是数据包套接 ...

  10. springboot 整合thymeleaf 书笔记

    pom.xml依赖添加 <!--引入thymeleaf--> <dependency> <groupId>org.springframework.boot</ ...