gpt4 book ai didi

android - YouTubePlayer - 如何处理点击监听器?

转载 作者:太空狗 更新时间:2023-10-29 13:05:49 30 4
gpt4 key购买 nike

我有一个在那里播放视频的布局,我想处理这个播放器上的 onClick。在我的线性布局中,我有他们的 api 中建议的 youtube fragment :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/youtubePlayerFragment"/>
</LinearLayout>

然后我在我的 Activity 中访问这个 fragment :

mYouTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtubePlayerFragment);

然后我初始化它:

    mYouTubePlayerFragment.initialize(CategoriesTabsData.DEVELOPER_KEY, new com.google.android.youtube.player.YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(com.google.android.youtube.player.YouTubePlayer.Provider provider, com.google.android.youtube.player.YouTubePlayer youTubePlayer, boolean b) {
// loading the video
}

@Override
public void onInitializationFailure(com.google.android.youtube.player.YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

}
});

我想在点击播放器时将“点击”打印到 Log.d。因此,当播放视频并点击/触摸其播放器时,我想将“点击”打印到日志中

我正在尝试处理对播放器的点击(我在 onInitializationSuccess 中获得了 youtubeplayer 对象)但我没有成功。

我试过这个:

    mYouTubePlayerFragment.getView().setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("youtube", "click");
}
});

但它没有达到 onClick。然后我尝试创建扩展 YouTubePlayerFragment 并实现 View.OnClickListener 的 MyYouTubePlayerFragment,因此我在我的 xml 中使用适当的名称(com.mypackagename.MyYouTubePlayerFragment)设置 fragment 并将 mYouTubePlayerFragment 的类型设置为 MyYouTubePlayerFragment 但它没有达到 onClick。

如何“捕捉”视频点击?

最佳答案

因此,要在父 View 中监听点击事件,您应该将点击事件从 subview 传递给父 View 。在谷歌 documentation有几种方法可以做到这一点,并且有一些辅助方法可以用于更高级别的 View ,例如 Activity、Fragment、ViewGroup、ViewParent:

There are some other methods that you should be aware of, which are not part of the View class, but can directly impact the way you're able to handle events. So, when managing more complex events inside a layout, consider these other methods:

Activity.dispatchTouchEvent(MotionEvent) - This allows your Activity to intercept all touch events before they are dispatched to the window.

ViewGroup.onInterceptTouchEvent(MotionEvent) - This allows a ViewGroup to watch events as they are dispatched to child Views.

ViewParent.requestDisallowInterceptTouchEvent(boolean) - Call this upon a parent View to indicate that it should not intercept touch events with onInterceptTouchEvent(MotionEvent).

关于android - YouTubePlayer - 如何处理点击监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48240996/

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