title: OnLongItemClick in RecyclerView
body: <p>I am starting to use RecyclerView in Android. Things work fine until I implement touch listener for my adapter.</p>
  
  <p>From this topic:</p>
  
  <p><a href="https://stackoverflow.com/a/26826692/2923403">https://stackoverflow.com/a/26826692/2923403</a></p>
  
  <p>I can implement the listener for childview correctly. However, I would like to implement both OnItemClick and OnItemLongClick, then problem appear. They are always fired at the same time. OnItemClick first and OnItemLongClick later, which renders my function useless.</p>
  
  <p>Here is my code for the custom listener (based on the code above, thanks to Fouad):</p>
  
  <pre><code>public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
  private OnItemClickListener mListener;
  
  public static interface OnItemClickListener {
      public boolean onItemClick(View view, int position);
  
      public void onItemLongClick(View view, int position);
  }
  
  GestureDetector mGestureDetector;
  
  public RecyclerItemClickListener(Context context, final RecyclerView recyclerView, OnItemClickListener listener) {
      mListener = listener;
      mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
          @Override
          public boolean onSingleTapUp(MotionEvent e) {
              return true;
          }
  
          @Override
          public void onLongPress(MotionEvent e) {
              View childView = recyclerView.findChildViewUnder(e.getX(), e.getY());
  
              if (childView != null &amp;&amp; mListener != null) {
                  mListener.onItemLongClick(childView, recyclerView.getChildPosition(childView));
              }
          }
  
      });
  }
  
  @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.getChildPosition(childView));
          return true;
      }
      return false;
  }
  
  @Override
  public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
  }
  </code></pre>
  
  <p>And for the implementation:</p>
  
  <pre><code>mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(),   mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
  @Override
   public boolean onItemClick(View view, int position) {
  //This is fired 1st
  }
   @Override
   public void onItemLongClick(View view, int position) {
  //This comes later
  }));
  </code></pre>
  
  <p>Do you have any solution for that?</p>