搜索:
首页 >> 软件编程 >> android编程 >> Android 中RecycleView实现item的点击事件

Android 中RecycleView实现item的点击事件

Asp之家 2019-1-28 作者:yanglei0917 投递文章

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

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

原文链接:http://blog.csdn.net/yanglei0917/article/details/52227976

Tags:Android  RecycleView  item 
相关文章
手机版 android编程 Asp之家 Aspxhome.com
闽ICP备06017341号