欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 中RecycleView实现item的点击事件

程序员文章站 2023-12-11 11:37:46
android 中recycleview实现item的点击事件 recycleview现在已经越来越受到大家的重视,因为他既可以代替listview还可以代替gridvi...

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

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