首页 > 解决方案 > 当 listitem 刚刚可见时,recyclerview 中的 Videoview 开始播放

问题描述

我将 VideoView 放入 RecyclerView 但不是直接放入。我使用带有页眉和页脚的容器,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!-- Header -->
    <TextView
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center"
        android:text="HEADER"
        android:textSize="20sp"
        android:layout_alignParentTop="true"
        android:background="@color/design_default_color_primary"/>

    <!-- Content -->
    <RelativeLayout
        android:id="@+id/container_for_infeed"
        android:layout_below="@id/header"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- FrameLayout for VideoView -->
        <FrameLayout
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:id="@+id/display_io_add"
            android:layout_width="match_parent"
            android:layout_height="350dp">

        </FrameLayout>

    </RelativeLayout>

    <!-- Footer -->
    <TextView
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center"
        android:text="FOOTER"
        android:textSize="120sp"
        android:layout_below="@id/container_for_infeed"
        android:background="@color/colorAccent"/>


</RelativeLayout> 

使用 VideoView 布局

视频在此布局出现在屏幕上时开始播放,并在滚动到下一个位置时停止。这对我来说不是很好,因为我在同一个容器中也有页脚和页眉,而且它们在 VideoView 之前肯定是可见的。以下是页脚刚特别可见并且视频开始时的滚动位置示例: 在此处输入图像描述

这是我的代码:

  videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        Log.e(TAG, "onPrepared,  videoView.getVisibility() =   "  + videoView.getVisibility());
                        if (!mp.isPlaying() && (videoView.getVisibility() == View.VISIBLE)) {
                            mp.start();
                        }
                    }
                });

问题是为什么 videoView 即使不在屏幕上也被视为可见?为什么每次页脚可见时都会调用 onPrepared ?

标签: androidandroid-recyclerviewandroid-videoview

解决方案


推荐阅读