用到RecyclerView的点击事件查的,记录一下,来源于 android v7兼容包RecyclerView的使用(四)——点击事件的不同方式处理
package com.mama.kanzhihudaily.ClickListener;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by dev on 10/31/15.
*/
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
private GestureDetector mGestureDetector;
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
public RecyclerItemClickListener(Context context,final RecyclerView recyclerView, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.OnGestureListener() {
@Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return true;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
View view = recyclerView.findChildViewUnder(motionEvent.getX(),motionEvent.getY());
if (view != null && mListener != null){
mListener.onItemLongClick(view,recyclerView.getChildAdapterPosition(view));
}
}
@Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View view = rv.findChildViewUnder(e.getX(),e.getY());
if (view != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(view,rv.getChildAdapterPosition(view));
return true;
}else{
return false;
}
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
</pre><pre name="code" class="java">使用:
<pre name="code" class="java">recyclerView<span style="font-family: Arial, Helvetica, sans-serif;">.addOnItemTouchListener(new RecyclerItemClickListener(this,gridView,listener));</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="java"> RecyclerItemClickListener.OnItemClickListener listener = new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
Log.d("tony onItemClick",position+"");
}
@Override
public void onItemLongClick(View view, int position) {
Log.d("tony onItemLongClick",position+"");
}
};