其实可以在一篇文章中写完这6种方式,但是我自己的习惯——不喜欢阅读太长的文章,硬生生的被我截断了,其实主要是这几种没有什么联系,分开读也无所谓。继续剩下的3种:
4.定义一个自己的RecyclerView类
见代码:
//初始化
private void init() {
addOnChildAttachStateChangeListener(new OnChildAttachStateChangeListener() {
@Override//当子View被添加到Window
public void onChildViewAttachedToWindow(View view) {
//此处参数的view就是ItemView
//这里可以这么实现
view.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//这里就可以回调自己定义的接口方法了
}
});
}
@Override//当子View从Window中移除/解绑
public void onChildViewDetachedFromWindow(View view) {
//此处参数的view就是ItemView
//那么这里我们可以做些什么事了?
//两种方案:
//1.啥也不做;
// do nothing
//2.把view的点击事件取消
view.setOnClickListener(null);
}
});
}
出自简书:http://www.jianshu.com/p/3070695619b9
其实和上一篇是一个人写的,我喜欢简单粗暴,于是直接放了链接,还有这种方法我还没试,明天写个demo试试。
5.RecyclerView 已有的方法 addOnItemTouchListener()+GestureDetectorCompat
(1)为RecyclerView添加addOnItemTouchListener
查看 RecyclerView 源码可以看到, RecyclerView 预留了一个Item的触摸事件方法:
/**
* Add an {@link OnItemTouchListener} to intercept touch events before they are dispatched
* to child views or this view's standard scrolling behavior.
*
* <p>Client code may use listeners to implement item manipulation behavior. Once a listener
* returns true from
* {@link OnItemTouchListener#onInterceptTouchEvent(RecyclerView, MotionEvent)} its
* {@link OnItemTouchListener#onTouchEvent(RecyclerView, MotionEvent)} method will be called
* for each incoming MotionEvent until the end of the gesture.
*
* @param listener Listener to add
* @see SimpleOnItemTouchListener
*/
public void addOnItemTouchListener(OnItemTouchListener listener) {
mOnItemTouchListeners.add(listener);
}
代码如下:
recyclerView.addOnItemTouchListener(new OnItemTouchListener(recyclerView) {
@Override
public void onItemClick(RecyclerView.ViewHolder vh) {
//item 操作
}
});
通过注释我们可知,此方法是在滚动事件之前调用.需要传入一个 OnItemTouchListener 对象. OnItemTouchListener 的代码如下:
public static interface OnItemTouchListener {
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e);
public void onTouchEvent(RecyclerView rv, MotionEvent e);
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept);
}
第一个是拦截触摸事件的,第二个是处理触摸事件的,第三个是处理触摸冲突的。第三个这里我们用不到,不用管,至于前两个我们只要向下面我们用到的GestureDetectorCompat传递MotionEvent为了获取触摸的坐标,最后再定义个abstract回调。
此接口还提供了一个实现类,且官方推荐使用该实现类 SimpleOnItemTouchListener
/**
* An implementation of {@link RecyclerView.OnItemTouchListener} that has empty method bodies and
* default return values.
* You may prefer to extend this class if you don't need to override all methods. Another
* benefit of using this class is future compatibility. As the interface may change, we'll
* always provide a default implementation on this class so that your code won't break when
* you update to a new version of the support library.
*/
public static class SimpleOnItemTouchListener implements RecyclerView.OnItemTouchListener {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
在触摸接口中,当触摸时会回调一个 MotionEvent 对象,通过使用 GestureDetectorCompat 来解析用户的操作。
(2)写一个 ItemClickListener
类继承 SimpleOnItemTouchListener
,构造时传入 RecyclerView
对象和Item点击回调,并覆写父类的 boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e)
方法,具体代码如下:
/**
* 点击事件
*/
public class ItemClickListener extends RecyclerView.SimpleOnItemTouchListener {
private OnItemClickListener clickListener;
private GestureDetectorCompat gestureDetector;
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public ItemClickListener(final RecyclerView recyclerView,
OnItemClickListener listener) {
this.clickListener = listener;
gestureDetector = new GestureDetectorCompat(recyclerView.getContext(),
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (childView != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onItemClick(childView, recyclerView.getChildAdapterPosition(childView));
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (childView != null && clickListener != null) {
clickListener.onItemLongClick(childView,
recyclerView.getChildAdapterPosition(childView));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
gestureDetector.onTouchEvent(e);
return false;
}
}
在 GestureDetectorCompat 的手势回调中我们覆写:
boolean onSingleTapUp(MotionEvent e)
void onLongPress(MotionEvent e)
(3)使用事件监听
在 RecyclerView 的对象中添加 addOnItemTouchListener() 方法,然后在回调中处理你需要的事件:
recyclerView.addOnItemTouchListener(new SingleItemClickListener(recyclerView,
new SingleItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
DevLog.i("touch click name:" + position);
Toast.makeText(SingleActivity.this, "touch click:" + position, Toast.LENGTH_SHORT).show();
}
@Override
public void onItemLongClick(View view, int position) {
DevLog.i("touch long click:" + position);
Toast.makeText(SingleActivity.this, "touch long click:" + position, Toast.LENGTH_SHORT).show();
}
}));
6.当 ItemView attach RecyclerView 时实现
代码如下:
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);
}
}
上面的代码中给 RecyclerView 设置了 OnChildAttachStateChangeListener 事件监听,当子 View attach RecyclerView 时设置事件监听。为了使代码独立简洁,可以写在单独的类中,无论是监听 ItemView 还是ItemView 中每个子 View 的点击事件。
出自;http://www.littlerobots.nl/blog/Handle-Android-RecyclerView-Clicks/
最后,欢迎大家吐槽,共同进步,谢谢。