Android 中RecycleView实现item的点击事件

Android 中RecycleView实现item的点击事件

RecycleView现在已经越来越受到大家的重视,因为他既可以代替listView还可以代替GridView,但是RecycleView本身不不像ListView那样具有setOnItemClickListener,这个关于子item的点击,但是我们往往会用到RecycleView并且希望他的自孩子可以被点击,那么如何实现他的item的点击事件呢?

首先我们在RecyclerView.ViewHolder中的实现:

public class GridViewHolder extends RecyclerView.ViewHolder implements OnClickListener{
  
  //声明MyItemClickListener
  private MyItemClickListener mListener; 
  public GridViewHolder(View rootView,MyItemClickListener listener) {
    super(rootView);
    this.mListener = listener; 
    rootView.setOnClickListener(this);
    
  }
  @Override
  public void onClick(View v) {
//如果mListener不为空,就实现接口中的方法onItemClick其中getPosition()是得到被点击位置的position
     if(mListener != null){ 
        mListener.onItemClick(v,getPosition()); 
      } 
  }
   
//声明MyItemClickListener这个接口
    public interface MyItemClickListener { 
    public void onItemClick(View view,int postion); 
  } 
}

最后在RecyclerViewAdapter中实现:

package com.ledon.activity.adapter;

import Java.util.ArrayList;
import java.util.List;

import com.ledon.activity.adapter.GridViewHolder.MyItemClickListener;
import com.ledon.ledongym.R;
import com.lidroid.xutils.BitmapUtils;

import Android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

public class RecyclerViewAdapter extends RecyclerView.Adapter<GridViewHolder> {
  private final List<String> labels;
  private LayoutInflater mLayoutInflater;
  private Context context;
  private MyItemClickListener mItemClickListener;

  
  public RecyclerViewAdapter(int count, Context context) {
    this.labels = new ArrayList<String>(count);
    mLayoutInflater = LayoutInflater.from(context);
    for (int i = 0; i < count; i++) {
      labels.add(String.valueOf(i));
    }
  }

  @Override
  public int getItemCount() {
    return labels.size();
  }

  

  @Override
  public void onBindViewHolder(GridViewHolder viewHolder, int position) {
    viewHolder.itemView.setTag(labels.get(position));
   

  }

  @Override
  public GridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
    View view = null;
   
    view = mLayoutInflater.inflate(R.layout.item_userinfo, parent, false);
   //通过构造函数将mItemClickListener
    GridViewHolder holder = new GridViewHolder(view, mItemClickListener);
    return holder;
  }

  /**
   * 设置Item点击监听
   * 
   * @param listener
   */
  public void setOnItemClickListener(MyItemClickListener listener) {
    this.mItemClickListener = listener;
  }

  
  
}

最后在activity中

//根据adapter来设置item的点击事件
    recyclerViewAdapter.setOnItemClickListener(new MyItemClickListener() {
      
      @Override
      public void onItemClick(View view, int postion) {
        
      }
    });

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

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#niaoge.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。