gpt4 book ai didi

android - 如何将手势检测器设置为 YoutubePlayerFragment

转载 作者:行者123 更新时间:2023-11-29 00:14:32 25 4
gpt4 key购买 nike

我正在使用 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);

但覆盖 block YoutubePlayerFragment。这个问题有解决方案吗?

最佳答案

您可以直接在 YouTubePlayerFragment 对象上设置手势监听器。尝试在您的 onCreate 方法中设置它...

YouTubePlayerFragment frag = (YouTubePlayerFragment) 
getFragmentManager().findFragmentById(R.id._________)
View v = frag.getView();
v.setOnTouchListener(new View.OnTouchListener() {
//your code goes here
}

只要搜索栏和播放按钮覆盖被隐藏,这就会起作用。如果不是,则覆盖会消耗手势。这很烦人,但除了将播放器样式设置为 chromeless 之外,我不知道有什么好的解决方法。这可以通过 mPlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);

来完成

当然,这意味着您不能有播放按钮或搜索栏,但您可以通过某种方式实现自己的。

关于android - 如何将手势检测器设置为 YoutubePlayerFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27655411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com