RecyclerView中单个item里面的子视图的点击监听

 数据源:datas = new ArrayList<>();

        for (int i = 0; i < 200; i++) {
            datas.add("aaaaa" + i);
        }
RecyclerView的一些设置:
 recycle = view.findViewById(R.id.recycle);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recycle.setLayoutManager(linearLayoutManager);
        recycle.setHasFixedSize(true);


/**
 * 声明接口变量
 */
private onItemClick onItemClick;

 
  
 /**
     * 定义监听接口  tag是关键
     */
    public static interface onItemClick {

        void onItemClick(int tag, View view, int position);
    }


 
  
/**
 * 声明给外界的方法
 *
 * @param listener
 */
public void setOnItemClickListener(onItemClick listener) {
    this.onItemClick = listener;
}

 
  
完整的代码:
package com.zdl.testapp;

import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by zdl on 2017/10/22.
 */

public class MyadApter extends RecyclerView.Adapter {

    private List datas;

    public MyadApter(List datas) {
        this.datas = datas;
    }


    /**
     * 声明接口变量
     */
    private onItemClick onItemClick;

    /**
     * 定义监听接口tag是区分点击的什么,position是位置,要想获取position还需要在重新设置下tag
     */
    public static interface onItemClick {

        void onItemClick(int tag, View view, int position);
    }

    /**
     * 声明给外界的方法
     *
     * @param listener
     */
    public void setOnItemClickListener(onItemClick listener) {
        this.onItemClick = listener;
    }

    /**
     * 绑定视图
     *
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public MyvViewholder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null, false);
        MyvViewholder myvViewholder = new MyvViewholder(view);
//        view.setOnClickListener(this);
        myvViewholder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClick.onItemClick(-55, view, (Integer) view.getTag());
            }
        });
        myvViewholder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClick.onItemClick(-66, view, (Integer) view.getTag());
            }
        });
        return myvViewholder;
    }

    /**
     * 填充数据
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(MyvViewholder holder, int position) {
        holder.textView.setText(datas.get(position));
        holder.itemView.setTag(position);//设置position的tag
        holder.textView.setTag(position);
        holder.imageView.setTag(position);

    }


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

    class MyvViewholder extends RecyclerView.ViewHolder {
        private TextView textView;
        private ImageView imageView;

        public MyvViewholder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
            imageView = itemView.findViewById(R.id.iv);
        }
    }


}


 
  
adapter的使用:
    MyadApter myadApter = new MyadApter(datas);
        myadApter.setOnItemClickListener(new MyadApter.onItemClick() {
            @Override
            public void onItemClick(int tag, View view, int position) {
                switch (tag) {
                    case -55:
                        Toast.makeText(getActivity(), "aaaaaa" +position, Toast.LENGTH_SHORT).show();
                        break;
                    case -66:
                        Toast.makeText(getActivity(), "bbbb" + position, Toast.LENGTH_SHORT).show();
                        break;
                }
            }


        });
        recycle.setAdapter(myadApter);


 
  

你可能感兴趣的:(控件使用)