Android中RecyclerView点击,长按事件详解

时间:2021-08-26 22:36:27

RecyclerView是support.v7包中的控件,可以说是ListView和GridView的增强升级版。

实现了ViewHolder的复用优化布局,还将onCreatViewHolder和onBindViewHolder分离高度解耦

缺点就是:点击事件的丢了,这就很尴尬了...


本篇就来介绍一下ReyclerView点击事件的实现

先来看看效果图:


Android中RecyclerView点击,长按事件详解             Android中RecyclerView点击,长按事件详解

单击,长按 都是点击,一起放过来说了Android中RecyclerView点击,长按事件详解

1.第一种,当然想到的还是接口回调(高端大气上档次)

/*习惯在Adapter中定义接口*/
//条目的点击
public interface OnItemClickListener {
void onClick(int position);
}

//长按的点击事件
public interface OnItemLongCilckListener {
void onLongClick(int position);
}

2.在Adapter中

①声明接口的引用②触发接口③将所需的position的值传到接口中

private OnItemClickListener itemClickListener;
private OnItemLongCilckListener onItemLongCilckListener;

public void addOnItemClickListener (OnItemClickListener itemClickListener){
this.itemClickListener = itemClickListener;
}

public void addItemLongClickListener (OnItemLongCilckListener onItemLongCilckListener){

this.onItemLongCilckListener = onItemLongCilckListener;
}

onBindViewHolder中设置item点击事件

@Override
public void onBindViewHolder (ViewHolder holder,int position){
holder.mImageView.setImageResource(mImageId.get(position));
holder.itemView.setOnClickListener(this);
holder.itemView.setOnLongClickListener(this);
}

要获取点击的position,用它的父容器进行获取子控件View得到,adapter中没有RecyclerView需要声明

单击事件:

private RecyclerView mRecyclerView;
public void setmRecyclerView (RecyclerView mRecyclerView){
this.mRecyclerView = mRecyclerView;
}
@Override
public void onClick (View v){
int position = mRecyclerView.getChildAdapterPosition(v);
itemClickListener.onClick(position);
}

长按事件:

@Override
public boolean onLongClick(View v) {
int position = mRecyclerView.getChildAdapterPosition(v);
onItemLongCilckListener.onLongClick(position);
return true;
}

在Main方法中为RecyclerView赋值并触发接口,将数据回调

ImageAdapter.setmRecyclerView(mRecycler);
ImageAdapter.addOnItemClickListener(new RvImageAdapter.OnItemClickListener() {
@Override
public void onClick(int position) {
Toast.makeText(MainActivity.this, "点击的位置是:" + position, Toast.LENGTH_SHORT).show();
data.remove(position);
ImageAdapter.notifyItemRemoved(position);
}
});

长按的回调:

public void onLongClick(int position) {
Toast.makeText(this, "长按的位置:" + position, Toast.LENGTH_SHORT).show();
int id = integer.getAndIncrement();
int mipmap_id = getResources().getIdentifier("p" + id, "mipmap", getPackageName());
data.add(position,mipmap_id);
ImageAdapter.notifyItemInserted(position);
}

-------------------------分割线---下面将介绍更简单的点击方式----------------------------------

ecyclerView在创建布局时,与Fragment类似,内部自带有onAttachedToRecyclerView和onDetachedFromRecyclerView

在这里可以进行RecyclerView的初始化,免去set方法再从Mian中的adapter中获取

就可以代替接口回调等复杂操作!!!

注意:为了避免内存泄漏,最后记得取消掉(它们是成对使用的)

@Override
public void onAttachedToRecyclerView (RecyclerView recyclerView){
super.onAttachedToRecyclerView(recyclerView);
this.mRecyclerView = recyclerView;
}

@Override
public void onDetachedFromRecyclerView (RecyclerView recyclerView){
super.onDetachedFromRecyclerView(recyclerView);
this.mRecyclerView = null;
}

也可以直接在Adapter中定义移除,添加的方法

然后在Main中直接使用Adapter.remove()....进行直接操作