当前位置:网站首页>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;}边栏推荐
猜你喜欢
随机推荐
一文带你快速掌握Kotlin核心技能
LLVM系列第二十四章:用Xcode编译调试LLVM源码
ARMv8虚拟化
可以拖拽的ViewGroup,仿微信拖拽缩放关闭
ConstraintLayout从入门到放弃
流,向量场,和微分方程
vscode编译keil工程,烧录程序
FP6195耐压60V电流降压3.3V5V模块供电方案
VS2017中安装visual assist X插件
PyTorch⑦---卷积神经网络_非线性激活
checkPermissions Missing write access to /usr/local/lib
arm push/pop/b/bl汇编指令
关于UDF
LLVM系列第十七章:控制流语句for
【我的电赛日记(三)】STM32学习笔记与要点总结
In the Visual studio code solutions have red wavy lines
LLVM系列第二十六章:理解LLVMContext
Must-know knowledge about disassembly
国内IT市场还有发展吗?有哪些创新好用的IT运维工具可以推荐?
5.使用RecyclerView优雅的实现瀑布流效果









