Click button inside row in StaggeredGrid RecyclerView causes whole item to click

Hi I have a button inside a row RecyclerView

(for a bookmark). When I click on a button, it calls that button as well as itemclick for the entire row. I have separate code to handle clicks of a button and the whole element. Why does a button in a row cause the entire element to be clicked?

How can I fix this problem?

Here is my code for a mouse click inside the adapter:

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

      

Here is my code for clicking on the RecyclerView item:

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) {
    }
}

      

+3


source to share


1 answer


Adding a touch listener for lists and using them ClickListeners

is not a good idea.
Here is a small example for setting an itemClick listenerRecyclerView

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 button listener on 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) {

            }
        });
    }

      

+3


source







All Articles