RecyclerView点击事件与长按事件,并添加点击效果

时间:2021-12-04 22:32:25

1.添加点击效果

为了让5.0以上的系统产生水波纹效果,所以我们新建一个drawable-v21目录来存放drawable文件 



RecyclerView点击事件与长按事件,并添加点击效果




5.0以下drawable文件

recycler_bg.xml 

[code]<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape android:shape="rectangle">
<solid android:color="#cfd8dc"></solid>
</shape>
</item>
</selector>




5.0以上 drawable-v21  

recycler_rectangle.xml 

recycler_bg.xml 

[code]<!--点击出现的水波纹效果是矩形的 -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>


[code]<?xml version="1.0" encoding="utf-8"?>
<!-- ripple 是5.0才出现的新标签-->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#cfd8dc" ><!-- 点击出现的水波纹的颜色 -->
<item android:drawable="@drawable/recycler_rectangle"/>
</ripple>

在网上找了一下实现方法,有的说在adapter里添加回调接口来实现点击事件与长按事件。 
不过经过测试,有时候会产生这样的现象:点击item跳转到下一个页面,再返回回来时, 
item的点击事件会失效。后来在Stack Overflow找到了新的方法。 
原文链接如下:RecyclerView onClick。这里 贴上代码:

public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
public interface OnItemClickListener {
void onItemClick(View view, int position);

void onItemLongClick(View view, int position);
}

private OnItemClickListener mListener;

private GestureDetector mGestureDetector;

public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;

mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}

@Override
public void onLongPress(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());

if (childView != null && mListener != null) {
mListener.onItemLongClick(childView, recyclerView.getChildAdapterPosition(childView));
}
}
});
}

@Override
public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());

if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
}

return false;
}

@Override
public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
  • 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

然后在activity中添加事件即可

recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
// ...
}

@Override
public void onItemLongClick(View view, int position) {
// ...
}
}));
</pre><br /><br />