我有为RecyclerVIEw处理singleClickListener的类.我正在使用GestureDetector处理点击.如何将LongPressListener添加到同一类中以同时处理它们?
public class RecyclerVIEwItemClickListener implements RecyclerVIEw.OnItemtouchListener { private OnItemClickListener mListener; public interface OnItemClickListener { public voID onItemClick(VIEw vIEw, int position); } GestureDetector mGestureDetector; public RecyclerVIEwItemClickListener(Context context, OnItemClickListener Listener) { mListener = Listener; mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @OverrIDe public boolean onSingleTapUp(MotionEvent e) { return true; } @OverrIDe public boolean onDown(MotionEvent event) { // triggers first for both single tap and long press return true; } @OverrIDe public voID onLongPress(MotionEvent event) { //... super.onLongPress(event); } }); } @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) { }
我在活动中使用此类的类:
recyclerVIEw.addOnItemtouchListener( new RecyclerVIEwItemClickListener(this, new RecyclerVIEwItemClickListener.OnItemClickListener() { @OverrIDe public voID onItemClick(VIEw vIEw, int position) { // Todo Handle item click Log.d(TAG, "item clicket position: " + position); } }) );
解决方法:
如果您想在onIntercepttouchEvent上都处理它们,只需像这样使用它.
public class RecyclerItemClickListener implements RecyclerVIEw.OnItemtouchListener {private OnItemClickListener mListener;private VIEw childVIEw;private RecyclerVIEw vIEwRecycle;public interface OnItemClickListener { voID onItemClick(VIEw vIEw, int position); voID onLongItemClick(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) { if (childVIEw != null && mListener != null){ mListener.onItemClick(childVIEw, vIEwRecycle.getChildAdapterposition(childVIEw)); } return true; } @OverrIDe public voID onLongPress(MotionEvent e) { if (childVIEw != null && mListener != null) { mListener.onLongItemClick(childVIEw, vIEwRecycle.getChildAdapterposition(childVIEw)); } super.onLongPress(e); } });}@OverrIDepublic boolean onIntercepttouchEvent(RecyclerVIEw vIEw, MotionEvent e) { AppHelper.AppLogger("insIDe"); childVIEw = vIEw.findChildVIEwUnder(e.getX(), e.getY()); vIEwRecycle = vIEw; mGestureDetector.ontouchEvent(e); return false;}@OverrIDepublic voID ontouchEvent(RecyclerVIEw vIEw, MotionEvent motionEvent) {}@OverrIDepublic voID onRequestdisallowIntercepttouchEvent(boolean disallowIntercept) {}
总结 以上是内存溢出为你收集整理的使用Android中的GestureDetector处理singleClick和LongPress全部内容,希望文章能够帮你解决使用Android中的GestureDetector处理singleClick和LongPress所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)