亲宝软件园·资讯

展开

RecycleView实现item的点击事件 Android 中RecycleView实现item的点击事件

yanglei0917 人气:0
想了解Android 中RecycleView实现item的点击事件的相关内容吗,yanglei0917在本文为您仔细讲解RecycleView实现item的点击事件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,中RecycleView实现item的点击事件,Android,中RecycleView,Android,中RecycleView点击事件,下面大家一起来学习吧。

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) {
        
      }
    });

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

加载全部内容

相关教程
猜你喜欢
用户评论