如何在容器片段视图中创建具有全屏和肖像功能的YouTube视图



我有一个活动和framelayout容器,用于更换活动中的片段。

<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootCo_Ordinator_Layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#efefef"
    android:orientation="vertical"
    android:fitsSystemWindows="false"
    tools:context=".DetailsActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <include layout="@layout/app_bar" />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <FrameLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@id/scroll_view_horizontal" />

            <HorizontalScrollView
                android:id="@+id/scroll_view_horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:layout_above="@id/second_row">
                <TextView
                    android:id="@+id/tv_path"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ellipsize="none"
                    android:padding="8dp"
                    android:scrollbars="horizontal"
                    android:singleLine="true"
                    android:textColor="@android:color/black"
                    android:textStyle="italic|bold"
                    tools:text="Sample Text" />
            </HorizontalScrollView>

            <LinearLayout
                android:id="@+id/second_row"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="#000000"
                android:orientation="horizontal"
                android:paddingBottom="4dp"
                android:weightSum="5">
                <Button
                    android:id="@+id/btn_content"
                    style="@style/DetailButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    android:text="@string/btn_desc"
                    android:textAllCaps="false" />
                <Button
                    android:id="@+id/btn_sub_cat"
                    style="@style/DetailButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    android:text="@string/btn_cat"
                    android:textAllCaps="false" />
                <Button
                    android:id="@+id/btn_text"
                    style="@style/DetailButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    android:text="@string/btn_cmntry"
                    android:textAllCaps="false" />
                <Button
                    android:id="@+id/btn_video"
                    style="@style/DetailButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="@string/btn_videos"
                    android:textAllCaps="false" />
                <Button
                    android:id="@+id/btn_image"
                    style="@style/DetailButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    android:text="@string/btn_images"
                    android:textAllCaps="false" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
    <androidx.core.widget.ContentLoadingProgressBar
        android:id="@+id/details_loading"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/transparent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

我还有另一个动态创建 youtubeplayerview 的片段,

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:fitsSystemWindows="false"
    android:scrollbars="vertical">
    <LinearLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/root_element"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:scrollbars="vertical"
        android:fitsSystemWindows="false"
        tools:context=".fragments.IntroFragment">
        <include
            layout="@layout/empty_view"
            android:visibility="gone" />
    </LinearLayout>
</ScrollView>

我有

下面的全屏utul类
public class FullScreenHelper {
private Activity context;
private View[] views;
public FullScreenHelper(Activity context, View ... views) {
    this.context = context;
    this.views = views;
}
public void enterFullScreen(View selectedView, View[] allViews) {
    View decorView = context.getWindow().getDecorView();
    hideSystemUi(decorView);
    for(View view : views) {
        if (!(view instanceof FrameLayout) && (view != selectedView) ) {
            view.setVisibility(View.GONE);
            view.invalidate();
        }
    }
    for (View view:allViews){
        if (view != null && (view != selectedView)  ){
            view.setVisibility(View.GONE);
            view.invalidate();
        }
    }
}

public void exitFullScreen(View selectedView, View[] allViews) {
    View decorView = context.getWindow().getDecorView();
    showSystemUi(decorView);
    for(View view : views) {
        view.setVisibility(View.VISIBLE);
        view.invalidate();
    }
    for (View view:allViews){
        if (view != null && (view != selectedView)  ){
            view.setVisibility(View.VISIBLE);
            view.invalidate();
        }
    }
}
private void hideSystemUi(View mDecorView) {
    mDecorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
private void showSystemUi(View mDecorView) {
    mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
}
}

,我捕获了切换并在下面的活动中进行更改,

  FullScreenHelper fsHelper = 
            new FullScreenHelper(this,mAppBar,mPathTextView,mHorizontalScrollView,mSecondRow);
       @Override
        public void toLandScape(View selectedView, View... allViews) {
              isLandscape = true;
              this.selectedView = selectedView;
              this.allViews = new View[allViews.length];
              this.allViews = allViews;
              setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
              getSupportActionBar().hide();
              fsHelper.enterFullScreen(selectedView,allViews);
         }
         @Override
         public void toPortrait(View selectedView, View... allViews) {
               isLandscape = false;
               setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
               getSupportActionBar().show();
              fsHelper.exitFullScreen(selectedView,allViews);
          }

可以在片段中动态创建许多 YouTubePlayerViews 按下切换屏幕时,我添加了相应的侦听器。当用户按当前视图中的切换时, YouTube-player应填充屏幕,并应该隐藏所有其他视图,再次按下切换时,应带回隐藏的视图。但是我无法实现这一目标。目前,所有的观点都在景观方向变化上隐藏,并显示了肖像的所有观点。对我如何进行此操作的任何帮助将不胜感激。!!!

转到任何YouTube视频

https://www.youtube.com/watch?v=m8ZoxzX0hro       #this is how the url looks 

现在,为了使其全屏并在您的应用中使用它,您必须在URL上进行更改

https://www.youtube.com/embed?v=m8ZoxzX0hro 

手表已被嵌入

替换

最新更新