RecyclerView添加条目点击事件setOnItemClickListener,不是在Adapter中设置;

时间:2023-07-11 11:01:32

RecyclerView不像ListView,可以直接写setOnItemClickListener,我们大部分都是在Adapter中的设置点击事件,这个是使用RecyclerView的addOnItemTouchListener方法,利用手势监听器实现条目点击事件;

public class VpRecyView extends RecyclerView{

    public VpRecyView(Context context) {
super(context);
init();
} public VpRecyView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
} private void init() {
this.addOnItemTouchListener(new RecyclerItemClickListener(this.getContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
if (onpagerClickListener != null) onpagerClickListener.onPagerClick(view,position);
} @Override
public void onLongClick(View view, int posotion) {
if (onpagerLongClickListener != null) onpagerLongClickListener.onPagerLongClick(view,posotion);
}
}));
} interface onPagerClickListener{
void onPagerClick(View view,int position);
}
private onPagerClickListener onpagerClickListener;
public void setOnPagerClickListener(onPagerClickListener onpagerClickListener){
this.onpagerClickListener = onpagerClickListener;
} interface onPagerLongClickListener{
void onPagerLongClick(View view,int position);
}
private onPagerLongClickListener onpagerLongClickListener;
public void setOnPagerLongClickListener(onPagerLongClickListener onpagerLongClickListener){
this.onpagerLongClickListener = onpagerLongClickListener;
}
}
RecyclerItemClickListener:这个类使用GestureDetector实现点击、双击、长按等的监听;
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private GestureDetector mGestureDetector;
private View childView;
private RecyclerView touchView; public RecyclerItemClickListener(Context context, final OnItemClickListener mListener) {
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent ev) {
if (childView != null && mListener != null) {
mListener.onItemClick(childView, touchView.getChildPosition(childView));
}
return true;
} @Override
public void onLongPress(MotionEvent ev) {
if (childView != null && mListener != null) {
mListener.onLongClick(childView, touchView.getChildPosition(childView));
}
}
});
} public interface OnItemClickListener {
void onItemClick(View view, int position); void onLongClick(View view, int posotion);
} @Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
mGestureDetector.onTouchEvent(e);
childView = rv.findChildViewUnder(e.getX(), e.getY());
touchView = rv;
return false;
} @Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) { } @Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { }
}

这样就能和ListView的条目点击事件一样使用了,不需要写在Adapter中; 我是从其它代码块中抽取出来的,还可以再简化;

   rv.setOnPagerClickListener(new VpRecyView.onPagerClickListener() {
@Override
public void onPagerClick(View view, int position) {
Log.i(TAG, "onPagerClick: "+datas1.get(position));
}
});

在Adapter中如何实现条目点击事件,这个就比较简单了;在BaseAdapter中写一个接口监听就行了;

interface OnItemClickListener {
void onItemClick(View view, int position);
}
private OnItemClickListener mOnItemClickListener;
public RecyclerAdapter setOnItemClickListener(OnItemClickListener listener) {
this.mOnItemClickListener = listener;
return this;
}
 if (null != mOnItemClickListener) {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mOnItemClickListener.onItemClick(v, holder.getAdapterPosition());
}
});
}