当前位置:网站首页>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;}
边栏推荐
- Enhanced Apktool reverse artifact
- LLVM系列第二十三章:写一个简单的运行时函数调用统计器(Pass)
- 蓝牙温度检测系统(基于BT08-B蓝牙模块)
- MapReduce流程
- Handler你真的搞懂了吗?
- Binder ServiceManager解析
- checkPermissions Missing write access to /usr/local/lib
- PyTorch⑦---卷积神经网络_非线性激活
- The overlapping effect of the two surfaceviews is similar to the video and handout practice in the live effect
- 文本匹配任务
猜你喜欢
随机推荐
FP5207电池升压 5V9V12V24V36V42V大功率方案
2020-02-06-快速搭建个人博客
Bert系列之 Transformer详解
The problem that UIWindow's makeKeyAndVisible does not call viewDidLoad of rootviewController
Redis database related commands
1. What is RecyclerView
ConstraintLayout从入门到放弃
spark资源调度和任务调度
Redis数据库相关指令
流,向量场,和微分方程
profiler network乱码
图像配置分类及名词解释
2.RecyclerView基本使用
LLVM系列第七章:函数参数Function Arguments
想做好分布式架构?这个知识点一定要理解透彻
拥抱Jetpack之印象篇
LLVM系列第六章:函数返回值Return
两个surfaceview的重叠效果类似直播效果中的视频和讲义实践
自定义圆形seekBar,超简单
对疫情期间量化策略表现的看法