Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Android RecyclerView

Android RecyclerView的Item点击事件实现整理

投稿:lqh

这篇文章主要介绍了Android RecyclerView的Item点击事件实现整理的相关资料,需要的朋友可以参考下

自从开始使用RecyclerView代替ListView,会发现有很多地方需要学习。前一段时间的学习记录有:

RecyclerView的滚动事件研究 - DevWiki
RecyclerView的ViewHolder和Adapter的封装优化 - DevWiki
RecyclerView问题记录 - DevWiki

实现 RecyclerView的Item的点击事件有三种方式:

  1. 在创建 ItemView时添加点击监听
  2. 当 ItemView attach RecyclerView时实现
  3. 通过RecyclerView已有的方法addOnItemTouchListener()实现

1.在创建ItemView时添加点击监听

      思路是:因为ViewHolder我们可以拿到每个Item的根布局,所以如果我们为根布局设置单独的OnClick监听并将其开放给Adapter,那不就可以在组装RecyclerView时就能够设置ItemClickListener,只不过这个Listener不是设置到RecyclerView上而是设置到Adapter。具体实现代码如下:

public class SampleAdapter extends RecyclerView.Adapter<SampleAdapter.SampleViewHolder> { 
 
  private List<DataBean> mDatas; 
  private OnItemClickListener mListener; // Item点击事件 
 
  public DataBean getItem(int position) { 
    return mDatas == null ? null : mDatas.get(position); 
  } 
 
  @Override 
  public SampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent,false); 
    return new SampleViewHolder(itemView); 
  } 
 
  @Override 
  public void onBindViewHolder(SampleViewHolder holder, int position) { 
 
  } 
 
  @Override 
  public int getItemCount() { 
    return mDatas == null ? 0 : mDatas.size(); 
  } 
 
  class SampleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { 
 
    public SampleViewHolder(View itemView) { 
      super(itemView); 
      // TODO:初始化View 
      ... 
 
      itemView.setOnClickListener(this); 
      itemView.setOnLongClickListener(this); 
    } 
 
    @Override 
    public void onClick(View v) { 
      if (mListener != null) { 
        mListener.onItemClick(SampleAdapter.this, v, getLayoutPosition()); 
      } 
    } 
 
    @Override 
    public boolean onLongClick(View v) { 
      if (mListener != null) { 
        mListener.onItemLongClick(SampleAdapter.this, v, getLayoutPosition()); 
        return true; 
      } 
      return false; 
    } 
  } 
} 

2.当ItemView attach RecyclerView时实现

      该实现方法是在阅读国外的一篇博客时发现的,原文链接如下:Getting your clicks on RecyclerView
实现的代码如下:

public class ItemClickSupport { 
 
  private static final int KEY = 0x99999999; 
  private final RecyclerView mRecyclerView; 
  private OnItemClickListener mOnItemClickListener; 
  private OnItemLongClickListener mOnItemLongClickListener; 
 
  private View.OnClickListener mOnClickListener = new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
      if (mOnItemClickListener != null) { 
        RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v); 
        mOnItemClickListener.onItemClicked(mRecyclerView, v, holder.getAdapterPosition()); 
      } 
    } 
  }; 
 
  private View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() { 
    @Override 
    public boolean onLongClick(View v) { 
      if (mOnItemLongClickListener != null) { 
        RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v); 
        return mOnItemLongClickListener.onItemLongClicked(mRecyclerView, v, holder.getAdapterPosition()); 
      } 
      return false; 
    } 
  }; 
 
  private RecyclerView.OnChildAttachStateChangeListener mAttachListener = new RecyclerView.OnChildAttachStateChangeListener() { 
 
    @Override 
    public void onChildViewAttachedToWindow(View view) { 
      if (mOnItemClickListener != null) { 
        view.setOnClickListener(mOnClickListener); 
      } 
      if (mOnItemLongClickListener != null) { 
        view.setOnLongClickListener(mOnLongClickListener); 
      } 
    } 
 
    @Override 
    public void onChildViewDetachedFromWindow(View view) { 
    } 
  }; 
 
  /** 
   * ItemClickSupport的私有构造方法 
   */ 
  private ItemClickSupport(RecyclerView recyclerView) { 
    mRecyclerView = recyclerView; 
    mRecyclerView.setTag(KEY, this); 
    // 为RecyclerView设置OnChildAttachStateChangeListener事件监听 
    mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener); 
  } 
 
  /** 
   * 为RecyclerView设置ItemClickSupport 
   */ 
  public static ItemClickSupport addTo(RecyclerView view) { 
    ItemClickSupport support = (ItemClickSupport) view.getTag(KEY); 
    if (support == null) { 
      support = new ItemClickSupport(view); 
    } 
    return support; 
  } 
 
  /** 
   * 为RecyclerView移除ItemClickSupport 
   */ 
  public static ItemClickSupport removeFrom(RecyclerView view) { 
    ItemClickSupport support = (ItemClickSupport) view.getTag(KEY); 
    if (support != null) { 
      support.detach(view); 
    } 
    return support; 
  } 
 
  /** 
   * 为RecyclerView设置点击事件监听 
   */ 
  public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) { 
    mOnItemClickListener = listener; 
    return this; 
  } 
 
  /** 
   * 为RecyclerView设置长按事件监听 
   */ 
  public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener) { 
    mOnItemLongClickListener = listener; 
    return this; 
  } 
 
  /** 
   * 为RecyclerView移除OnChildAttachStateChangeListener事件监听 
   */ 
  private void detach(RecyclerView view) { 
    view.removeOnChildAttachStateChangeListener(mAttachListener); 
    view.setTag(KEY, null); 
  } 
 
  /** 
   * RecyclerView的点击事件监听接口 
   */ 
  public interface OnItemClickListener { 
    void onItemClicked(RecyclerView recyclerView, View itemView, int position); 
  } 
 
  /** 
   * RecyclerView的长按事件监听接口 
   */ 
  public interface OnItemLongClickListener { 
    boolean onItemLongClicked(RecyclerView recyclerView, View itemView, int position); 
  } 
} 

      上面的代码中给RecyclerView设置了OnChildAttachStateChangeListener事件监听,当子View attach RecyclerView时设置事件监听。

private RecyclerView.OnChildAttachStateChangeListener mAttachListener = new RecyclerView.OnChildAttachStateChangeListener() { 
 
 @Override 
  public void onChildViewAttachedToWindow(View view) { 
    if (mOnItemClickListener != null) { 
      view.setOnClickListener(mOnClickListener); 
    } 
    if (mOnItemLongClickListener != null) { 
      view.setOnLongClickListener(mOnLongClickListener); 
    } 
  } 
  
  @Override 
  public void onChildViewDetachedFromWindow(View view) {} 
}; 

      使用时只需要调用addTo(RecycleView view)方法得到ItemClickSupport对象,然后调用setOnItemClickListener()方法和setOnItemLongClickListener()方法设置ItemView的点击事件和长按事件监听即可。

3.通过RecyclerView已有的方法addOnItemTouchListener()实现

3.1、查看源码

查看RecyclerView源码可以看到,RecyclerView预留了一个Item的触摸事件方法:

/** 
 * Add an {@link OnItemTouchListener} to intercept touch events before they are dispatched 
 * to child views or this view's standard scrolling behavior. 
 * 
 * <p>Client code may use listeners to implement item manipulation behavior. Once a listener 
 * returns true from 
 * {@link OnItemTouchListener#onInterceptTouchEvent(RecyclerView, MotionEvent)} its 
 * {@link OnItemTouchListener#onTouchEvent(RecyclerView, MotionEvent)} method will be called 
 * for each incoming MotionEvent until the end of the gesture.</p> 
 * 
 * @param listener Listener to add 
 * @see SimpleOnItemTouchListener 
 */ 
public void addOnItemTouchListener(OnItemTouchListener listener) { 
  mOnItemTouchListeners.add(listener); 
} 

      通过注释我们可知,此方法是在滚动事件之前调用,需要传入一个OnItemTouchListener对象。OnItemTouchListener的代码如下:

public static interface OnItemTouchListener {  
  
  public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e); 
  
  public void onTouchEvent(RecyclerView rv, MotionEvent e); 
  
  public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept); 
} 

此接口还提供了一个实现类,且官方推荐使用该实现类SimpleOnItemTouchListener:

/** 
 * An implementation of {@link RecyclerView.OnItemTouchListener} that has empty method bodies and 
 * default return values. 
 * 
 * You may prefer to extend this class if you don't need to override all methods. Another 
 * benefit of using this class is future compatibility. As the interface may change, we'll 
 * always provide a default implementation on this class so that your code won't break when 
 * you update to a new version of the support library. 
 */ 
public static class SimpleOnItemTouchListener implements RecyclerView.OnItemTouchListener { 
  <span style="font-family:'Microsoft YaHei';">  
</span>  @Override 
  public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 
    return false; 
  } 
  
  @Override 
  public void onTouchEvent(RecyclerView rv, MotionEvent e) { 
  } 
  
  @Override 
  public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 
  } 
} 

      在触摸接口中,当触摸时会回调一个MotionEvent对象,通过使用GestureDetectorCompat来解析用户的操作。

3.2、了解GestureDetector的工作原理

      对于触摸屏,其原生的消息无非按下、抬起、移动这几种,我们只需要简单重载onTouch或者设置触摸侦听器setOnTouchListener即可进行处理。不过,为了提高我们的APP的用户体验,有时候我们需要识别用户的手势,Android给我们提供的手势识别工具GestureDetector就可以帮上大忙了。

       GestureDetector的工作原理是,当我们接收到用户触摸消息时,将这个消息交给GestureDetector去加工,我们通过设置侦听器获得GestureDetector处理后的手势。

      GestureDetector提供了两个侦听器接口,OnGestureListener处理单击类消息,OnDoubleTapListener处理双击类消息。
OnGestureListener的接口有这几个:

// 单击,触摸屏按下时立刻触发  
abstract boolean onDown(MotionEvent e);  
// 抬起,手指离开触摸屏时触发(长按、滚动、滑动时,不会触发这个手势)  
abstract boolean onSingleTapUp(MotionEvent e);  
// 短按,触摸屏按下后片刻后抬起,会触发这个手势,如果迅速抬起则不会  
abstract void onShowPress(MotionEvent e);  
// 长按,触摸屏按下后既不抬起也不移动,过一段时间后触发  
abstract void onLongPress(MotionEvent e);  
// 滚动,触摸屏按下后移动  
abstract boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);  
// 滑动,触摸屏按下后快速移动并抬起,会先触发滚动手势,跟着触发一个滑动手势  
abstract boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);  
OnDoubleTapListener的接口有这几个:
// 双击,手指在触摸屏上迅速点击第二下时触发  
abstract boolean onDoubleTap(MotionEvent e);  
// 双击的按下跟抬起各触发一次  
abstract boolean onDoubleTapEvent(MotionEvent e);  
// 单击确认,即很快的按下并抬起,但并不连续点击第二下  
abstract boolean onSingleTapConfirmed(MotionEvent e);  

有时候我们并不需要处理上面所有手势,方便起见,Android提供了另外一个类SimpleOnGestureListener实现了如上接口,我们只需要继承SimpleOnGestureListener然后重载需要的手势即可。

3.3、实现点击事件监听

      了解了GestureDetector的工作原理之后,便开始实现RecycleView的Item的点击事件。首先写一个SimpleRecycleViewItemClickListener类继承SimpleOnItemTouchListener,构造时传入Item点击回调OnItemClickListener,并覆写父类的boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e)方法,具体代码如下:

/** 
 * RecyclerView的Item点击事件监听 
 * 
 * @author liyunlong 
 * @date 2016/11/21 9:42 
 */ 
public class SimpleRecycleViewItemClickListener extends RecyclerView.SimpleOnItemTouchListener { 
 
  private OnItemClickListener mListener; 
  private GestureDetectorCompat mGestureDetector; 
 
  public SimpleRecycleViewItemClickListener(OnItemClickListener listener) { 
    this.mListener = listener; 
  } 
 
  @Override 
  public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { 
    if (mGestureDetector == null) { 
      initGestureDetector(rv); 
    } 
    if (mGestureDetector.onTouchEvent(e)) { // 把事件交给GestureDetector处理 
      return true; 
    } else { 
      return false; 
    } 
  } 
 
  /** 
   * 初始化GestureDetector 
   */ 
  private void initGestureDetector(final RecyclerView recyclerView) { 
    mGestureDetector = new GestureDetectorCompat(recyclerView.getContext(), new GestureDetector.SimpleOnGestureListener() { // 这里选择SimpleOnGestureListener实现类,可以根据需要选择重写的方法 
 
      /** 
       * 单击事件 
       */ 
      @Override 
      public boolean onSingleTapUp(MotionEvent e) { 
        View childView = recyclerView.findChildViewUnder(e.getX(), e.getY()); 
        if (childView != null && mListener != null) { 
          mListener.onItemClick(childView, recyclerView.getChildLayoutPosition(childView)); 
          return true; 
        } 
        return false; 
      } 
 
      /** 
       * 长按事件 
       */ 
      @Override 
      public void onLongPress(MotionEvent e) { 
        View childView = recyclerView.findChildViewUnder(e.getX(), e.getY()); 
        if (childView != null && mListener != null) { 
          mListener.onItemLongClick(childView, recyclerView.getChildLayoutPosition(childView)); 
        } 
      } 
 
      /** 
       * 双击事件 
       */ 
      @Override 
      public boolean onDoubleTapEvent(MotionEvent e) { 
        int action = e.getAction(); 
        if (action == MotionEvent.ACTION_UP) { 
          View childView = recyclerView.findChildViewUnder(e.getX(), e.getY()); 
          if (childView != null && mListener != null) { 
            mListener.onItemDoubleClick(childView, recyclerView.getChildLayoutPosition(childView)); 
            return true; 
          } 
        } 
        return false; 
      } 
 
    }); 
 
  } 
 
  /** 
   * RecyclerView的Item点击事件监听接口 
   * 
   * @author liyunlong 
   * @date 2016/11/21 9:43 
   */ 
  public interface OnItemClickListener { 
 
    /** 
     * 当ItemView的单击事件触发时调用 
     */ 
    void onItemClick(View view, int position); 
 
    /** 
     * 当ItemView的长按事件触发时调用 
     */ 
    void onItemLongClick(View view, int position); 
 
    /** 
     * 当ItemView的双击事件触发时调用 
     */ 
    void onItemDoubleClick(View view, int position); 
  } 
 
 
  /** 
   * RecyclerView的Item点击事件监听实现 
   * 
   * @author liyunlong 
   * @date 2016/11/21 10:05 
   */ 
  public class SimpleOnItemClickListener implements OnItemClickListener { 
 
    @Override 
    public void onItemClick(View view, int position) { 
 
    } 
 
    @Override 
    public void onItemLongClick(View view, int position) { 
 
    } 
 
    @Override 
    public void onItemDoubleClick(View view, int position) { 
 
    } 
  } 
} 

      在GestureDetectorCompat的手势回调中我们覆写:

  1. boolean onSingleTapUp(MotionEvent e):单击事件回调
  2. void onLongPress(MotionEvent e):长按事件回调
  3. boolean onDoubleTapEvent(MotionEvent e):双击事件回调

      如果我们只需要监听单击事件,而不需要监听长按事件和双击事件,构造SimpleRecycleViewItemClickListener时只需要传入SimpleOnItemClickListener即可,如果需要处理其它的手势监听,也可以覆写对应的手势回调方法。

4.三种方法对比

以上三种方式分别是:

  1. 在创建ItemView时添加点击监听
  2. 当ItemView attach RecyclerView时实现
  3. 通过RecyclerView已有的方法addOnItemTouchListener()实现

从以上三种方式的实现过程可知:

三种均可实现ItemView的点击事件和长按事件的监听。

第一种和第二种方式可以很方便对ItemView中的子View进行监听。

第三种方式可以很方便获取用户点击的坐标。

第二种方式和第三种方式可以写在单独的类中,相对于第一种写在Adapter的方式可使代码更独立整洁。

综上所述:

      如果你只想监听ItemView的点击事件或长按事件,三种方式均可。

      如果你想监听ItemView中每个子View的点击事件,采用第一种或者第二种比较方便。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:
阅读全文