title: Handle both singleClick and LongPress with GestureDetector in Android
body: <p>I have class that handles singleClickListener for RecyclerView. I am using GestureDetector to handle clicks. How can I add LongPressListener to same class to handle them both?</p>
  
  <pre><code>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 &amp;&amp; mListener != null &amp;&amp; mGestureDetector.onTouchEvent(e)) {
              mListener.onItemClick(childView, view.getChildAdapterPosition(childView));
          }
          return false;
      }
  
      @Override
      public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
      }
  </code></pre>
  
  <p>And I use this class like this in my activity:</p>
  
  <pre><code>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);
                      }
                  })
          );
  </code></pre>