【发布时间】:2015-10-28 01:06:54
【问题描述】:
我正在使用YoutubePlayerFragment 类播放视频。我想添加一个GestureListener,但他们没有设置手势检测器的方法。我试过了:
<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>
和
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;
}
}
这就是我将手势检测器设置为 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);
但是覆盖会阻止YoutubePlayerFragment。有解决这个问题的办法吗?
【问题讨论】:
标签: android android-youtube-api android-gesture