Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android YouTube Player API Fragment doesn't provide ability to handle touch events manually

I'm using YouTubePlayerFragment to add embedded youtube video capabilities to an android app. The video preview (thumbnail) is displayed using a fragment which is part of a cell of a recycler view. Tap on the cell should activate the video playback (anywhere, not only fragment) and it works just great and activates video playback. Unfortunately the fragment itself intercepts all touches and doesn't allow me to activate the video on fragment tap.

I tried to add to the cell root android:clickable="true" and set up a touch listener - events don't come.

I have also tried to set up a touch listener on fragment view (fragment.View.setOnTouchListener) - the same effect, event don't come.

How can I intercept fragment tap and execute my custom code?

p.s. I have tried YouTubeThumbnailView and it doesn't work for me because the only opportunity to play the video is to use YouTubeStandalonePlayer and intent, which activates new activity while I want to stay within my current activity without a context switch.

like image 743
Alexey Strakh Avatar asked Jul 15 '16 01:07

Alexey Strakh


2 Answers

I was able to get it working by placing the YouTubePlayerFragment in a custom LinearLayout:

public class TouchEventLayout extends LinearLayout {

    public TouchEventLayout(Context context) {
        super(context);
    }

    public TouchEventLayout(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev);
    }
}
like image 125
donny.rewq Avatar answered Nov 11 '22 05:11

donny.rewq


I know this question is pretty old, but I've encountered the same problem.

The solution was to put YouTubePlayerFragment inside a custom layout (derived from e.g. LinearLayout) and override its onInterceptTouchEvent method:

@Override
public boolean onInterceptTouchEvent(MotionEvent motionEvent) {
    final int action = motionEvent.getAction();

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            // on touch started
            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            // on touch finished/cancelled
            break;

        case MotionEvent.ACTION_MOVE:
            // on touch moved
            break;
    }

    return false;
}
like image 30
pawello2222 Avatar answered Nov 11 '22 04:11

pawello2222