ViewBinding-不适用于子布局 [英] ViewBinding- not working for child layouts

查看:53
本文介绍了ViewBinding-不适用于子布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently updating the source code with the ViewBindings but I'm not getting them to work for child layouts of the other modules which was working previously. I'm on Android Studio 4.1.3. I added viewBinding.enabled = true to app modules build.gradle. But when I try to access a button from the child layouts, it does not give any error but it does not perform the operation also.

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

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

If anyone gets idea please let me know.

推荐答案

代替:

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

在您的 FragmentBindingProvider.kt 中:

In your FragmentBindingProvider.kt:

   binding.testPlayerView.btnMute

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

Because after you do binding.btnMute? and may be btnMute is null and setOnCheckedChangeListener won't be trigger. If it's not the solution remove your val btnMute: ToggleButton? to val btnMute: ToggleButton and remove binding.btnMute? by binding.btnMute. But nromaaly viewbinding is null safe so you have a value for your button.

这篇关于ViewBinding-不适用于子布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆