1

I am using a RecyclerView to display these CardViews with a custom layout. What I want to do is detect when the user clicks that play button, in addition to when they click the CardView. Currently I am using a RecyclerView.OnItemTouchListener, but it seems to be unable to yield all the events I need (the play button clicks.)

Screenshot

public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

private GestureDetector gestureDetector;
private ClickListener clickListener;

public interface ClickListener {
    void onClick(View view, int position);

    void onLongClick(View view, int position);
}

public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
    this.clickListener = clickListener;
    gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
            if (child != null && clickListener != null) {
                clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child));
            }
        }
    });
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

    View child = rv.findChildViewUnder(e.getX(), e.getY());

    if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
        clickListener.onClick(child, rv.getChildAdapterPosition(child));
    }
    return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}

}

This code only tells me when the CardView is clicked, I want the play button clicks as well. Should I just set onClick listeners in my adapter class even though this seems like a bad practice?

Thank you for your time.

Brandon Bahret
  • 163
  • 3
  • 10
  • You should create a custom item layout and a custom adapter. In the adapter, you should set the onclick for your button. Check out this answer http://stackoverflow.com/a/30285361/4872155 – Wilder Pereira Jan 03 '17 at 02:10
  • Possible duplicate of [Handle Button click inside a row in RecyclerView](http://stackoverflow.com/questions/30284067/handle-button-click-inside-a-row-in-recyclerview) – Wilder Pereira Jan 03 '17 at 02:10
  • It's not a duplicate as I was seeking a solution that didn't involve setting onClick listeners in the adapter class. Though I fear this will be the only solution here, it seems like this is less flexible than using the recommended RecyclerView.OnItemTouchListener. – Brandon Bahret Jan 03 '17 at 02:25
  • The logic you are trying to handle (events, view rects...) is already managed internally, through View.OnClickListener. Set the appropriate listeners in the adapter, when binding data. – natario Jan 03 '17 at 03:31
  • I think I will probably end up doing just that, oh well. I guess RecyclerView.OnItemTouchListener wasn't meant for this purpose. – Brandon Bahret Jan 03 '17 at 03:40

2 Answers2

1

I was hoping to find something that worked with RecyclerView.OnItemTouchListener as it seems to be the proper way to achieve this. In the end I think the best way is to just set the onClick listeners in the adapter class for the RecyclerView (which is what I'll be doing.)

Thanks for the help either way.

This was a post suggested by one of the commentators as an alternative to what I was planning on doing.

Handle Button click inside a row in RecyclerView

Community
  • 1
  • 1
Brandon Bahret
  • 163
  • 3
  • 10
-1

I'm not sure about this (cause I was doing the opposite, click on child click the parent), but android:clickable="false" android:focusable="false" may help you: set your root view to not clickable/focusable, then add a click listener on your button, without any intercept.

Hope this helps.

Robin Thoni
  • 1,651
  • 13
  • 22