1. Create an interface in the Adapter, define the methods inside the interface, and implement the interface in the Adapter

`public void setOnItemClickListener(OnItemClickListener listener) { this.mListener=listener; }

public interface OnItemClickListener { void onItemClick(int position); } `

`public class MySecondAdapter extends RecyclerView.Adapter<MySecondAdapter.InnerHolder> {

private final Context mContext;
private OnItemClickListener mListener;


public MySecondAdapter(Context context){
    this.mContext=context;
}

@NonNull
@Override
public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = View.inflate(mContext, R.layout.item_my_recycler_view, null);
    return new InnerHolder(view);
}

@Override
public void onBindViewHolder(@NonNull InnerHolder holder, final int position) {
    holder.mTextView.setText("我是 "+position+" 号");
    holder.mImageView.setImageResource(R.mipmap.ic_launcher_round);

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mListener.onItemClick(position);
        }
    });
}


public void setOnItemClickListener(OnItemClickListener listener) {
    this.mListener=listener;
}

public interface OnItemClickListener {
    void onItemClick(int position);
}

@Override
public int getItemCount() {
    return 30;
}

public class InnerHolder extends RecyclerView.ViewHolder {

    private final TextView mTextView;
    private final ImageView mImageView;

    public InnerHolder(@NonNull View itemView) {
        super(itemView);
        mTextView = itemView.findViewById(R.id.tv_title);
        mImageView = itemView.findViewById(R.id.iv_pic);

    }
}
Copy the code

} `

2. #### set click events for itemView

holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mListener.onItemClick(position); }});

3. Set the click event to itemView

mMyAdapter.setOnItemClickListener(new MySecondAdapter.OnItemClickListener() { @Override public void onItemClick(int position) { Toast.makeText(MyRecyclerViewActivity.this,"position is "+position,Toast.LENGTH_SHORT).show(); }});

Code: gitee.com/gonnainsane…