Button click inside row in StaggeredGrid RecyclerView triggers whole item click

1.6k views Asked by At

Hi I have a button inside RecyclerView row(for Bookmarking). When I click on the button it triggers that button click and also itemclick for the whole row. I have seperate code for handling clicks of the button and the whole item. Why is the buttonclick in the row triggering whole item click?

How can I fix this issue?

Here is my code for button click inside adapter:

viewHolder.imgBookmark.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        ...
    }
});

Here is my code for item click of RecyclerView:

mGridView.addOnItemTouchListener(new RecyclerItemClickListener(
        getActivity(), mGridView,
        new RecyclerItemClickListener.OnItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        ...
    }

    @Override
    public void onItemLongClick(View view, int position) {
        ...
    }
}));

RecyclerItemClickListener.java

public class RecyclerItemClickListener implements
    RecyclerView.OnItemTouchListener {
    public static interface OnItemClickListener {
        public void onItemClick(View view, int position);

        public void onItemLongClick(View view, int position);
    }

    private OnItemClickListener mListener;
    private GestureDetector mGestureDetector;

    public RecyclerItemClickListener(Context context,
            final RecyclerView recyclerView, OnItemClickListener listener) {
        mListener = listener;

        mGestureDetector = new GestureDetector(context,
                new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                return true;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                View childView = recyclerView.findChildViewUnder(
                        e.getX(), e.getY());

                if (childView != null && mListener != null) {
                    mListener.onItemLongClick(childView,
                            recyclerView.getChildPosition(childView));
                }
            }
        });
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
        View childView = view.findChildViewUnder(e.getX(), e.getY());

        if (childView != null && mListener != null
                && mGestureDetector.onTouchEvent(e)) {
            mListener.onItemClick(childView, view.getChildPosition(childView));
        }

        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) {
    }
}
1

There are 1 answers

7
Kartheek On

Adding the touch listener for the listitems and using them ClickListeners is not a good idea.
Here is small example for setting the itemClick listener of the RecyclerView

public static class ViewHolder extends RecyclerView.ViewHolder implements OnClickListener {
public IMyViewHolderClicks mListener;

public ViewHolder(View itemLayoutView, IMyViewHolderClicks listener) {
    super(itemLayoutView);
    mListener = listener;
    itemLayoutView.setOnClickListener(this);
   }
}

Updated :
Adding the Listener of the button in the list item.

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //Set the data for the views.
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }