recyclerViewClick
recyclerView item Click Listener
现在很多地方提倡用recyclerView来代替listview,因为
recyclerView的很多特性是很方便的,而且更美观简洁。
但是在它里面却没有listview的itemlistener,所以,要自己
去创建一个RecyclerItemClickListener,方法实现如下:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@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;
}
}
实现了RecyclerView.OnItemTouchListener接口。
当想要调用的时候,需要这样利用:
recyclerView.addOnItemTouchListener(
new RecyclerItemClickListener (context, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
//TODO your shings
}
})
);
这样就可以实现应用,recyclerView和card一块使用,把card作为item,
这样是很好的。
chenzhao@hustunique.com