Home » Android » java – Recyclerview addOnItemTouchListener get whichsubview is clicked inside row

java – Recyclerview addOnItemTouchListener get whichsubview is clicked inside row

Posted by: admin June 15, 2020 Leave a comment


I’ve implemented Recyclerview onclickListener from this Stack overflow solution. This solution works fine for the recycler item clicks. But I can’t able to get which subview(ex: ImageView,Button) is clicked from the row.

            new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
                public void onItemClick(View view, int position) {
                    if (view.getId()==R.id.attachmnet_remove) {

onItemClick(view,position) always returns view id as -1

How do I track whick view is clicked??

How to&Answers:

Below is a ViewHolder that contains two text views viz. title and description:

public class CustomViewHolder extends RecyclerView.ViewHolder {
    private final OnViewClickListener mListener;
    public final TextView title;
    public final TextView description;

    public interface OnViewClickListener {
        void onViewClick(View v, int adapterPosition);

    public CustomViewHolder(View itemView, OnViewClickListener mListener) {
        this.mListener = mListener;
        title = (TextView) itemView.findViewById(R.id.titleTextView);
        description = (TextView) itemView.findViewById(R.id.descriptionTextView);



    private final View.OnClickListener onClickListener = new View.OnClickListener() {
        public void onClick(View view) {
            mListener.onViewClick(view, getAdapterPosition());

Both of these subviews have an OnClickListener attached to them that calls the custom OnViewClickListener implementation passing the View that was clicked as well as the position of the RecyclerView item in the adapter that received the click event.

Finally use View.getId() to retrieve the clicked view’s id in OnViewClickListener implementation.


That answer is outdated in that it uses RecyclerView.getChildPosition(View) which is deprecated. Try replacing it with RecyclerView.getChildAdapterPosition(View).

If I was you, I would just create listeners for every ViewHolder and its child views that you want to listen to. Honestly, I don’t seem to see the benefit of using that more complicated method that you referenced. Also, that method will only tell you that an item was clicked, nothing about the item’s child views.

In addition, according to the docs, RecyclerView.OnItemTouchListener is meant to be used when you want to detect a touch to an item while the list is scrolling, not for regular or direct touches.


Set Tags

You can set Tag to your views while initializing them via:


This will set the view Tag to 1.

then In onItemClick method:

public void onItemClick(View view, int position) {
 //do something
 //do something else


Try to add onClickListeners to the subviews.