Hiten's Blog.

分享一个RecyclerView的click技巧

字数统计: 661阅读时长: 3 min
2016/11/23 Share

如何处理一个RecyclerView列表点击事件,在网上一般能搜到两种方案:

  • 从ViewHolder中得到rootView,然后进行setOnClickListener();

  • 实现RecyclerView.OnItemTouchListener接口,从Touch事件中分发出Click事件;

这两种解决方案在网上都有讲解,想了解更多的,可以参考这篇博客,我在实际开发中,更倾向于第一种方案,因为这种方案比较简单,但是每次还得在ViewHoler中写代码,很多时候点击事件的逻辑应该在activity中实现,这样还得定义用于回调的接口,然后从activity得到viewHoler对象,set事件,作为一个有洁癖的程序员,这是多么邋遢的写法,但更可耻的是,我竟然一直在默默接受了;

今天在闲逛国外大牛博客时,发现一种封装的技巧,也是基于view的setOnClickListener方法,但不是在viewHolder获取view;

先看看怎么调用:

1
2
3
4
5
6
ItemClickSupport.addTo(mRecyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
// do it
}
});

ItemClickSupport源码也很简单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
public class ItemClickSupport {
private final RecyclerView mRecyclerView;
private OnItemClickListener mOnItemClickListener;
private OnItemLongClickListener mOnItemLongClickListener;
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
mOnItemClickListener.onItemClicked(mRecyclerView, holder.getAdapterPosition(), v);
}
}
};
private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (mOnItemLongClickListener != null) {
RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, holder.getAdapterPosition(), v);
}
return false;
}
};
private RecyclerView.OnChildAttachStateChangeListener mAttachListener
= new RecyclerView.OnChildAttachStateChangeListener() {
@Override
public void onChildViewAttachedToWindow(View view) {
if (mOnItemClickListener != null) {
view.setOnClickListener(mOnClickListener);
}
if (mOnItemLongClickListener != null) {
view.setOnLongClickListener(mOnLongClickListener);
}
}

@Override
public void onChildViewDetachedFromWindow(View view) {

}
};

private ItemClickSupport(RecyclerView recyclerView) {
mRecyclerView = recyclerView;
mRecyclerView.setTag(R.id.item_click_support, this);
mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);
}

public static ItemClickSupport addTo(RecyclerView view) {
ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
if (support == null) {
support = new ItemClickSupport(view);
}
return support;
}

public static ItemClickSupport removeFrom(RecyclerView view) {
ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
if (support != null) {
support.detach(view);
}
return support;
}

public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {
mOnItemClickListener = listener;
return this;
}

public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) {
mOnItemLongClickListener = listener;
return this;
}

private void detach(RecyclerView view) {
view.removeOnChildAttachStateChangeListener(mAttachListener);
view.setTag(R.id.item_click_support, null);
}

public interface OnItemClickListener {

void onItemClicked(RecyclerView recyclerView, int position, View v);
}

public interface OnItemLongClickListener {

boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);
}
}

同时,你还需要在ids.xml中定义一个名字为item_click_support的id:

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="item_click_support" type="id" />
</resources>

或者,你可以把R.id.item_click_support换成一个自己明白的int数值,这样就不用定义id了;

注意:请在不需要监听时或者activity销毁前,remove掉callBack

感谢代码出处作者,感谢大家的阅读,如果使用中有什么bug,欢迎一起探讨!

代码出处:http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks

CATALOG