BottomSheetDialog 内 VideoView 的 MediaController 隐藏在 BottomSheetDialog 后面

Posted

技术标签:

【中文标题】BottomSheetDialog 内 VideoView 的 MediaController 隐藏在 BottomSheetDialog 后面【英文标题】:MediaController for VideoView inside BottomSheetDialog is hidden behind the BottomSheetDialog 【发布时间】:2021-12-14 09:01:06 【问题描述】:

我有一个项目,我以编程方式将自定义地图标记添加到地图活动。当用户单击标记时,BottomSheetDialog 会从屏幕底部出现,其中包含有关自定义地图标记的信息。关键信息之一是将播放的视频。问题是 VideoView 的 MediaController 没有悬停在视频上方,而是在 BottomSheetDialog 后面。它位于屏幕上的正确位置,但位于 BottomSheetDialog 和 VideoView 的后面。我正在使用 EpoxyRecyclerView 将 VideoView、MediaController 和各种 TextView 添加到 BottomSheetDialog。

创建BottomSheetDialog的函数:

override fun onMarkerClick(marker: Marker): Boolean 
    val modalSheetView = layoutInflater.inflate(R.layout.activity_page_details, null)
    val bottomSheetDialog = BottomSheetDialog(this)
    bottomSheetDialog.setContentView(modalSheetView)

    pageController.page = marker.tag as Page

    val epoxyRecyclerView = bottomSheetDialog.findViewById<EpoxyRecyclerView>(R.id.pageDetailsRecyclerView)
    epoxyRecyclerView?.setControllerAndBuildModels(pageController)

    bottomSheetDialog.show()

    bottomSheetDialog.setOnDismissListener  it.dismiss() 

    // Return false to indicate that we have not consumed the event and that we wish
    // for the default behavior to occur (which is for the camera to move such that the
    // marker is centered and for the marker's info window to open, if it has one).
    return false

Epoxy 函数将 VideoView 和 MediaController 添加到 BottomSheetDialog:

data class PageVideoEpoxyModel(
    val videoUrl: String
):ViewBindingKotlinModel<ActivityPageVideoBinding>(R.layout.activity_page_video)

    override fun ActivityPageVideoBinding.bind() 
        videoView.setVideoURI(Uri.parse(videoUrl))
        videoView.setOnPreparedListener 

            val mediaController = MediaController(videoView.context)

            mediaController.setMediaPlayer(videoView)
            mediaController.isEnabled = true

            videoView.setMediaController(mediaController)

            mediaController.setAnchorView(videoView)
            mediaController.requestFocus()

            videoView.start()
            mediaController.show(0)
        
    

VideoView 的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_>

    <VideoView
        android:id="@+id/videoView"
        android:layout_
        android:layout_
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:clickable="true"/>

</androidx.constraintlayout.widget.ConstraintLayout>

EpoxyRecycler 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
tools:context=".MapsActivity"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<com.airbnb.epoxy.EpoxyRecyclerView
    android:layout_
    android:layout_
    android:id="@+id/pageDetailsRecyclerView"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

【参考方案1】:

我设法通过在我的 VideoView 的布局文件中添加 FrameLayout 来解决这个问题:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_>

<VideoView
    android:id="@+id/videoView"
    android:layout_
    android:layout_
    android:layout_marginTop="16dp"
    android:clickable="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<FrameLayout
    android:id="@+id/frameView"
    android:layout_
    android:layout_
    android:layout_gravity="bottom"
    app:layout_constraintStart_toStartOf="@id/videoView"
    app:layout_constraintEnd_toEndOf="@id/videoView"
    app:layout_constraintBottom_toBottomOf="@id/videoView"/>

</androidx.constraintlayout.widget.ConstraintLayout>

然后从其父视图中移除 MediaController 并将其添加到 FrameView。在 EpoxyController 中构建 VideoView 的方法中的代码:

data class PageVideoEpoxyModel(
    val videoUrl: String,
):ViewBindingKotlinModel<ActivityPageVideoBinding>(R.layout.activity_page_video)

    override fun ActivityPageVideoBinding.bind() 
        videoView.setVideoURI(Uri.parse(videoUrl))
        videoView.setOnPreparedListener 

            val mediaController = MediaController(videoView.context)
            mediaController.requestFocus()

            val parent = mediaController.parent as ViewGroup
                parent.removeView(mediaController)

            frameView.addView(mediaController)

            mediaController.setMediaPlayer(videoView)
            mediaController.isEnabled = true

            videoView.setMediaController(mediaController)

            mediaController.setAnchorView(videoView)

            videoView.start()
            mediaController.show(0)
        
    

【讨论】:

以上是关于BottomSheetDialog 内 VideoView 的 MediaController 隐藏在 BottomSheetDialog 后面的主要内容,如果未能解决你的问题,请参考以下文章

BottomSheetDialog控件使用

在 Android 中,BottomSheetDialog 未正确舍入顶角

如何在 BottomSheetDialog 中获取 ViewModel

如何在BottomSheetDialog android的顶部中心添加关闭按钮?

在 BottomSheetDialog Fragment 中将视图固定到屏幕底部

Android使用BottomSheetBehavior 和 BottomSheetDialog实现底部弹窗