我正在使用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
。有解决这个问题的方法吗?
答案 0 :(得分:1)
您可以在YouTubePlayerFragment对象上设置手势监听器。尝试使用...
在onCreate
方法中进行设置
YouTubePlayerFragment frag = (YouTubePlayerFragment)
getFragmentManager().findFragmentById(R.id._________)
View v = frag.getView();
v.setOnTouchListener(new View.OnTouchListener() {
//your code goes here
}
只要隐藏了搜索栏和播放按钮叠加层,它就会起作用。如果他们不是该重叠消耗手势。这很烦人,但除了将玩家风格设置为无铬外,我不知道如何解决它。这可以使用mPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
当然,这意味着您无法拥有播放按钮或搜索栏,但您可以以某种方式实现自己的。
答案 1 :(得分:1)
对于遇到相同问题的人,最好的方法是扩展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;
}
}
:
loadFooter()