WebView Youtube Полноэкранный режим на RecyclerView - PullRequest
0 голосов
/ 13 февраля 2020

Как мне сделать полноэкранное видео из RecyclerView?

Итак, я выполнил действие под названием

VideoActivity. 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
    }
}

activity_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>

Теперь вот адаптер для RecyclerView в VideoActivity

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();
    }
}

и вот XML для RecyclerView

cardview_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>

Теперь полноэкранный режим в значительной степени работает. Обратите внимание, что код для полного экрана взят из ответа на этот вопрос: Воспроизведение HTML5 видео в полноэкранном режиме в android webview . Я только немного подкорректировал, чтобы код работал на классе адаптера (добавлен ((VideoActivity) контекст) до .getwindow (), чтобы код работал)

Проблема в том, что FullScreen работает только на Область RecycleView, как вы видите в этом видео:

https://www.youtube.com/watch?v=HtOZ_9fPTVA

Как сделать видео на самом деле полноэкранным? Потому что сейчас полноэкранный режим работает только внутри RecycleView

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...