当前位置:网站首页>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.
LLVM系列第九章:控制流语句if-else
Pytorch(16)---搭建一个完整的模型
Binder机制(中篇)
Manifest merger failed with multiple errors, see logs
kotlin Android序列化
flutter中App签名
vscode编译keil工程,烧录程序
内存申请(malloc)和释放(free)之上篇
Ffmpeg交叉编译
图像配置分类及名词解释
不可不知的反汇编相关知识
NDK报错问题分析方案(一)
define #使用
LLVM系列第七章:函数参数Function Arguments
FP5139电池与适配器供电DC-DC隔离升降压电路反激电路电荷泵电路原理图
source /build/envsetup.sh和lunch)
The problem that UIWindow's makeKeyAndVisible does not call viewDidLoad of rootviewController
PyTorch(15)---模型保存和加载
LLVM系列第五章:全局变量Global Variable









