Android Studio中RecyclerView使用(三)---RecyclerView的点击事件

RecyclerView必须要能响应点击事件才可以,不同于ListView的是,,RecyclerView没有提供类似于setOnItemClickListener()这样的注册监听器的方法,而是需要自己去给子项具体的View去注册点击事件,
本文继承自
Android Studio中RecyclerView使用(一)
https://blog.csdn.net/weixin_43880133/article/details/96856086
Android Studio中RecyclerView使用(二)–实现横向滚动和瀑布流布局
https://blog.csdn.net/weixin_43880133/article/details/96865312
所用代码共用对象、属性

在RecyclerView中注册点击事件

修改FruitAdapter中的代码

package com.example.recycleviewtest;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
    private List<Fruit> mFruitList;
    static class ViewHolder extends RecyclerView.ViewHolder{

        View fruitView;

        ImageView fruitImage;
        TextView fruitName;
        public ViewHolder(View view){
            super(view);

            fruitView = view;

            fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
            fruitName = (TextView) view.findViewById(R.id.fruit_name);
        }
    }
    public FruitAdapter (List<Fruit> fruitList){
        mFruitList = fruitList;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);

        final ViewHolder holder = new ViewHolder(view);
        holder.fruitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Fruit fruit = mFruitList.get(position);
                Toast.makeText(view.getContext(),"you clicked view"+fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });

        holder.fruitImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Fruit fruit = mFruitList.get(position);
                Toast.makeText(view.getContext(),"you clicked image"+fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });

        return holder;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder,int position){
        Fruit fruit = mFruitList.get(position);
        holder.fruitImage.setImageResource(fruit.getImageId());
        holder.fruitName.setText(fruit.getName());
    }
    @Override
    public int getItemCount(){
        return mFruitList.size();
    }

1、先修改了ViewHolder,在ViewHolder中添加了fruitView变量来保存子项最外层布局的实例,然后在onCreateViewHolder()方法中注册点击事件就可以,这里分别是最外层布局和ImageView都注册了点击事件,通过两个点击事件先获取了用户点击的position,然后通过position拿到相应的Fruit实例,再使用Toast分别弹出两种不同的内容显示区别
2、以上代码中TextView没有设置点击事件,因此点击文字这个事件会被子项的最外层布局捕获到。

你可能感兴趣的:(AndroidStudio,RecyclerView)