【问题标题】:ViewBinding- not working for child layoutsViewBinding - 不适用于子布局
【发布时间】:2021-06-26 12:11:57
【问题描述】:

我目前正在使用 ViewBindings 更新源代码,但我没有让它们适用于以前工作的其他模块的子布局。我在Android Studio 4.1.3。我将viewBinding.enabled = true 添加到应用模块build.gradle。但是当我尝试从子布局中访问按钮时,它不会给出任何错误,但它也不会执行操作。

main_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">

<com.playerview.TestPlayerView
    android:id="@+id/testPlayerView"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    app:v3PlaybackEnabled="true"
    app:always_show_go_to_live="true"
    app:extra_overlay_layout = "@array/extra_overlays"
    app:server_side_ad_overlay_index = "1"
    app:hide_play_pause_on_live="true"
    app:show_partner_logo="true"
    app:hide_seek_controllers_on_live="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:show_subtitle_on_full_screen="true" />

</androidx.constraintlayout.widget.ConstraintLayout>

FragmentBindingProvider.kt

class FragmentBindingProvider private constructor(
val btnPlayNext: Button?,
val testPlayerView: TestPlayerView,
val txtPlaylistPosition: TextView?,
val btnMute: ToggleButton?,
private val root: View
) : ViewBinding {

override fun getRoot(): View = root

companion object {
    fun inflate(isLogoView: Boolean, inflater: LayoutInflater, container: ViewGroup?): FragmentBindingProvider {
        return if (isLogoView) initLogo(inflater, container) else init(inflater, container)
    }

    private fun initLogo(inflater: LayoutInflater, container: ViewGroup?): FragmentBindingProvider {
        val binding = FragmentLogoBinding.inflate(inflater, container, false)
        return FragmentBindingProvider(
            btnPlayNext = binding.btnPlayNext,
            testPlayerView = binding.testPlayerView,
            txtPlaylistPosition = binding.txtPlaylistPosition,
            btnMute = binding.testPlayerView.findViewById(R.id.btnMute),
            root = binding.root
        )
    }
  }
}

player_video_controls.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"
    tools:visibility="visible">

         <include
        android:id="@+id/player_volume_layout"
        layout="@layout/view_controls_volume"
        android:layout_width="@dimen/player_ad_volume_width"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@id/bottom_bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/bottom_bar" />

</androidx.constraintlayout.widget.ConstraintLayout>

view_controls_volume.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:background="@color/transparent_black_percent_80">

    <ToggleButton
        android:id="@+id/btnMute"
        android:layout_width="@dimen/top_controls_icon_width"
        android:layout_height="@dimen/top_controls_icon_height"
        android:layout_gravity="center"
        android:background="@drawable/ic_volume"
        android:contentDescription="@null"
        android:scaleType="center"
        android:checked="true"
        android:textOff=""
        android:textOn="" />

</FrameLayout>

MainFragment.kt

override fun onStart() {
    super.onStart()
    observeViewModel()

    /*Here calling view of child layout*/
    binding.btnMute?.setOnCheckedChangeListener { v, isChecked ->
        if (isChecked && binding.testPlayerView.isMuted()) {
            binding.testPlayerView.unmute()
        } else {
            binding.testPlayerView.mute()
        }
    }
}

如果有人知道,请告诉我。

【问题讨论】:

  • this 我的回答有帮助吗?
  • 不,@SomeshKumar 它没有帮助
  • 您是否尝试在运行时调试您生成的绑定?如果该按钮为空,则无法通过绑定在播放器视图上通过 ID 找到它。我假设您正在使用 player_video_controls 与 exoPlayer 使用其控制器布局的方式相同。如果是这种情况,请尝试在播放器视图构造函数上放置一个断点,看看它是否如何处理 player_video_controls

标签: android android-layout kotlin android-viewbinding


【解决方案1】:

只回答包含的布局在另一个模块的问题:

如果player_video_controls.xml 与player_video_controls.xml 不在同一个模块中,为了快速修复,请将其添加到同一个模块中,或者不包含它,而是添加布局本身(这很糟糕,但它有效)。

但是,如果您有很多此问题的实例,我建议您阅读此answer 并尝试在项目的每个模块中启用 ViewbBinding,这些模块是存在此问题的模块的直接或间接依赖项。

【讨论】:

  • 我从 ui 模块中复制了 app 模块中的 player_video_controls.xml 并重命名了该文件,但仍然无法正常工作
  • 你应该使用 binding.playerVolumeLayout.btnMute 来获取 ToggleButton 的句柄。
  • 它对您的问题有帮助吗?
【解决方案2】:

bindig.playerVolumeLayout.btnMute 你可以这样访问

【讨论】:

    【解决方案3】:

    而不是:

    binding.testPlayerView.findViewById(R.id.btnMute),
    

    在您的 FragmentBindingProvider.kt 中:

       binding.testPlayerView.btnMute
    

    因为在您执行binding.btnMute? 之后,可能btnMute 为空并且不会触发setOnCheckedChangeListener。 如果不是解决方案,请将您的val btnMute: ToggleButton? 删除到val btnMute: ToggleButton 并通过binding.btnMute 删除binding.btnMute?。但是 nromaaly viewbinding 是 null 安全的,所以你的按钮有一个值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 2021-01-19
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多