当前位置:网站首页>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核心技能
PyTorch(13)---优化器_随机梯度下降法
2. Basic use RecyclerView
【使用Pytorch实现VGG16网络模型】
tensorflow实战之手写体识别
关系代数、SQL与逻辑式语言
7.如何给RecyclerView添加Click和LongClick事件
LLVM系列第二十八章:写一个JIT Hello World
HAL框架
还是别看学位论文
Bash shell位置参数
两个surfaceview的重叠效果类似直播效果中的视频和讲义实践
【目标检测】YOLO v5 吸烟行为识别检测
The NDK portal: C
STM32F1和F4的区别
5.使用RecyclerView优雅的实现瀑布流效果
LLVM系列第四章:逻辑代码块Block
对疫情期间量化策略表现的看法
语言模型(NNLM)
LLVM系列第二十章:写一个简单的Function Pass