在recyclerview上查看youtube全屏模式

jfewjypa  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(237)

如何从recyclerview制作全屏视频?
所以我做了一个叫做

视频活动.java:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import java.util.HashMap;

public class VideoActivity extends AppCompatActivity {
    View v;

    TextView tvEmptyList;

    String id, name;

    ListItemVideoAdapter adapter;
    RecyclerView recyclerView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);

        v = findViewById(R.id.activity_video);

        id = getIntent().getStringExtra("id");
        name = getIntent().getStringExtra("name");

        setTitle(name);

        tvEmptyList = findViewById(R.id.tv_empty_list);

        recyclerView = findViewById(R.id.rvVideo);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(adapter);

        startConnection();
    }

    public void startConnection() {
        //Class for getting the video URL
    }
}

活动\u video.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_video"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@+id/tv_empty_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/no_video"
        android:layout_centerInParent="true"
        android:drawableTop="@drawable/ic_free_breakfast_black_48dp"
        android:visibility="gone"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvVideo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

下面是videoactivity中recyclerview的适配器

listitemvideoadapter.java:

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

class ListItemVideoViewHolder extends RecyclerView.ViewHolder {
    public final Context context;
    TextView title;
    VideoEnabledWebView webView;
    private VideoEnabledWebChromeClient webChromeClient;

    public ListItemVideoViewHolder(final View itemView) {
        super(itemView);
        context = itemView.getContext();
        title = itemView.findViewById(R.id.title);
        webView = itemView.findViewById(R.id.wvVideo);

        final Activity activity = new VideoActivity();

        View nonVideoLayout = itemView.findViewById(R.id.rv_row);
        ViewGroup videoLayout = (ViewGroup) itemView.findViewById(R.id.videoLayout);
        webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, null, webView) // See all available constructors...
        {
            // Subscribe to standard events, such as onProgressChanged()...
            @Override
            public void onProgressChanged(WebView view, int progress)
            {
                // Your code...
            }
        };
        webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback()
        {
            @Override
            public void toggledFullscreen(boolean fullscreen)
            {
                // Your code to handle the full-screen change, for example showing and hiding the title bar. Example:
                if (fullscreen)
                {
                    WindowManager.LayoutParams attrs = ((VideoActivity)context).getWindow().getAttributes();
                    attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                    ((VideoActivity)context).getWindow().setAttributes(attrs);
                    if (android.os.Build.VERSION.SDK_INT >= 14)
                    {
                        ((VideoActivity)context).getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
                    }
                }
                else
                {
                    WindowManager.LayoutParams attrs = ((VideoActivity)context).getWindow().getAttributes();
                    attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
                    attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
                    ((VideoActivity)context).getWindow().setAttributes(attrs);
                    if (android.os.Build.VERSION.SDK_INT >= 14)
                    {
                        ((VideoActivity)context).getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
                    }
                }

            }
        });
        webView.setWebChromeClient(webChromeClient);
    }
}

public class ListItemVideoAdapter extends RecyclerView.Adapter<ListItemVideoViewHolder> {
    ArrayList<HashMap<String, String>> videoList;
    Context context;

    public ListItemVideoAdapter(Context context, ArrayList<HashMap<String, String>> videoList) {
        this.context = context;
        this.videoList = videoList;
    }

    void changeItem(ArrayList<HashMap<String, String>> videoList) {
        this.videoList = videoList;
        notifyDataSetChanged();
    }

    public ListItemVideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ListItemVideoViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_video, parent, false));
    }

    public void onBindViewHolder(final ListItemVideoViewHolder holder, final int position) {

        holder.title.setText((this.videoList.get(position)).get("title"));
        holder.webView.loadData((this.videoList.get(position).get("url")), "text/html", "utf-8");
    }

    public int getItemCount() {
        return this.videoList.size();
    }
}

下面是用于recyclerview的xml

cardview\u video.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView android:id="@+id/cv_training_schedule"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginStart="12dp"
    android:layout_marginTop="12dp"
    android:layout_marginEnd="12dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout android:id="@+id/rv_row"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <id.co.midiatama.app.VideoEnabledWebView
            android:id="@+id/wvVideo"
            android:layout_width="match_parent"
            android:layout_height="240dp">
        </id.co.midiatama.app.VideoEnabledWebView>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/wvVideo"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            android:layout_marginBottom="6dp"
            android:layout_marginTop="6dp"
            android:text="@string/promo_title"
            android:textAlignment="center"
            android:textSize="18sp"
            android:textStyle="bold" />

    </RelativeLayout>

    <!-- View where the video will be shown when video goes fullscreen -->
    <RelativeLayout
        android:id="@+id/videoLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </RelativeLayout>
</android.support.v7.widget.CardView>

现在全屏几乎可以工作了。注意,全屏的代码来自这个问题的答案:在androidwebview中全屏播放html5视频。我只做了一些调整,这样代码就可以在适配器类上工作(在.getwindow()之前添加了((videoactivity)上下文),这样代码就可以工作了)
问题是全屏只能在recycleview区域工作,正如您在本视频中看到的:
https://www.youtube.com/watch?v=htoz_9fptva
如何使视频真正全屏?因为现在全屏只在循环视图中工作

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题