Reputation: 43
I am using the YoutubePlayerFragment
class to play videos. I want to add a GestureListener
, but their is no method to set a gesture detector. I tried:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:id="@+id/player_holder"
android:visibility="gone"
android:layout_weight="40">
<RelativeLayout
android:layout_width="match_parent"
android:duplicateParentState="true"
android:layout_height="match_parent">
<fragment
android:id="@+id/player_fragment"
android:duplicateParentState="true"
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/gestureCatcher"
android:orientation="vertical"
android:duplicateParentState="true"
android:layout_height="match_parent" />
</RelativeLayout>
</LinearLayout>
and
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) return false;
// right to left swipe
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(NonStopActivity.this, "Left Swipe", Toast.LENGTH_SHORT)
.show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(NonStopActivity.this, "Right Swipe", Toast.LENGTH_SHORT)
.show();
}
} catch (Exception e) {
// nothing
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
This is how I set the gesture detector to the LinearLayout
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
LinearLayout gestureCatcher = (LinearLayout) findViewById(R.id.gestureCatcher);
gestureCatcher.setOnTouchListener(gestureListener);
But the overlay blocks YoutubePlayerFragment
. Is there a solution around this problem?
Upvotes: 2
Views: 626
Reputation: 11177
For those who had the same issue, the best way is to extend YouTubePlayerFragment
:
public class DraggableYouTubePlayerFragment extends YouTubePlayerFragment {
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
final View view = super.onCreateView(layoutInflater, viewGroup, bundle);
FrameLayout wrapper = new FrameLayout(viewGroup.getContext()) {
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return true;
}
};
wrapper.addView(view);
return wrapper;
}
}
Upvotes: 1
Reputation: 31
You can set a gesture listener right on YouTubePlayerFragment object. Try setting it in your onCreate
method with...
YouTubePlayerFragment frag = (YouTubePlayerFragment)
getFragmentManager().findFragmentById(R.id._________)
View v = frag.getView();
v.setOnTouchListener(new View.OnTouchListener() {
//your code goes here
}
This will work as long as the seek bar and play button overlay are hidden. If they aren't that overlay consumes the gestures. It's annoying, but I don't know a good way around it except to set the player style to chromeless. This can be done with mPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
Of course, that means you can't have a play button or the seek bar, but you could implement your own somehow.
Upvotes: 1