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.
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);
}
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With