当前位置:网站首页>7. How to add the Click to RecyclerView and LongClick events
7. How to add the Click to RecyclerView and LongClick events
2022-08-02 15:12:00 【love learning】
/** * Author: Pich * Original link: http://me.woblog.cn/ * QQGroup: 129961195 * WeChat public account: woblog * Github: https://github.com/lifengsofts */
Detailed RecyclerView Series Article Directory
Overview
If you do Android development, then you must have heard that RecyclerView does not provide a default implementation of Click and LongClick events.I don't know what the official intention is, so this course will implement this function. The completed rendering is shown in the following figure:
Here we don't talk about excessive encapsulation, and now we just write it casually, the purpose is to make it easier to understand.A later course on encapsulation will cover further encapsulation.
Add OnItemClickListener
public interface OnItemClickListener {void onItemClick(ViewHolder holder,int position);}
Add an interface to call back to the location you need after the click event in the adapter.
implement the click event in the adapter
public void onBindViewHolder(final CardViewActivity.MyAdapter.MyViewHolder holder,final int position) {String d = CardViewActivity.this.data.get(position);holder.bindData(d, position);if (onItemClickListener != null) {holder.itemView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {onItemClickListener.onItemClick(holder, position);}});}}
Here we first judge whether the onItemClickListener is empty, and then set the click leave, which saves more resources than setting the click listener as soon as it comes up, and then judging whether the listener is empty in the click event.
handle click event
Then you can handle the click event in the callback method.
String s = data.get(position);Intent intent = new Intent(this, NewsDetailActivity.class);intent.putExtra(NewsDetailActivity.URL, s);startActivity(intent, options.toBundle());
The long press event is also very simple. You can directly change the above listener, add a method, or create an interface. Let's create another interface here.
create OnItemLongClickListener
public interface OnItemLongClickListener {boolean onItemLongClick(ViewHolder holder,int position);}
It is important to note that we passed the ViewHolder in the callback. The advantage of this is that we can get the corresponding view in the click event, and we can directly perform some animations without getting the View again in findViewById.
handle long press event
There is no need to say more about this, everyone basically knows it, but it should be noted that onItemLongClick has a return value, and he judges whether OnItemClick is being called according to the return, so this place must return different values according to the actual situation.
@Overridepublic boolean onItemLongClick(ViewHolder holder, int position) {Toast.makeText(this, "Long Click", Toast.LENGTH_SHORT).show();return true;}
边栏推荐
- 深度学习之文本分类总结
- AAPT: error: duplicate value for resource ‘attr/xxx‘ with config ‘‘, file failed to compile.
- Win10不能启动WampServer图标呈橘黄色的解决方法
- 没学好统计学的下场
- What?It's 2020, you still can't adapt the screen?
- 内存申请(malloc)和释放(free)之上篇
- define #使用
- Enhanced Apktool reverse artifact
- Tensorflow常用函数
- Cannot figure out how to save this field into database. You can consider adding a type converter for
猜你喜欢
随机推荐
流,向量场,和微分方程
基于51单片机和物联网的智能家居系统(ESP8266物联网模块)
PyTorch(14)---使用现有的模型及其修改
使用预训练语言模型进行文本生成的常用微调策略
RN开发时遇到的问题
2. Basic use RecyclerView
spark中RDD与DF的关系
基于GPT的隐变量表征解码结构
FP7195转模拟调光技术解决智能家居调光频闪和电感噪音的原理
一文带你快速掌握Kotlin核心技能
Win10 can't start WampServer icon is orange solution
基于深度学习的配准框架
7.如何给RecyclerView添加Click和LongClick事件
【目标检测】YOLO v5 安全帽检测识别项目模型
ARMv8虚拟化
预训练模型 Bert
UIWindow的makeKeyAndVisible不调用rootviewController 的viewDidLoad的问题
LLVM系列第二十八章:写一个JIT Hello World
PyTorch⑤---卷积神经网络_卷积层
LLVM系列第十八章:写一个简单的IR处理流程Pass