android - 如何在 SupportMapFragment 中使用 View 绑定(bind)?

标签 android android-viewbinding

如何在 SupportMapFragment 中使用 View 绑定(bind)?引用我的代码
我在布局中有一个 map fragment ,我想在 fragment 中使用 View 绑定(bind)。

val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment


LZ需要解决方案..
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.SupportMapFragment
import com.zeddigital.zigmaster.R
import com.zeddigital.zigmaster.databinding.FragmentHomeBinding

class HomeFragment : Fragment() {
    private lateinit var mMap: GoogleMap

    private var binding : FragmentHomeBinding ?=null
    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View {
        binding = FragmentHomeBinding.inflate(inflater)

        // binding.myTextView.text = "sample"

        val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
        mapFragment.getMapAsync { googleMap ->

        }

        return binding!!.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        binding=null
    }
}





<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.home.HomeFragment">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

最佳答案

上面的答案是正确的,但是对于最新的 Android Studio,我发布了一些代码更改。
从现在开始推荐使用<androidx.fragment.app.FragmentContainerView />而不是 <fragment /> .
现在您可以使用如下 View 绑定(bind):
对于 Java:

SupportMapFragment mapFragment = (SupportMapFragment) childFragmentManager.findFragmentById(binding.map.getId());
对于 Kotlin:
val mapFragment = childFragmentManager.findFragmentById(binding.map.id) as SupportMapFragment

关于android - 如何在 SupportMapFragment 中使用 View 绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66927320/

相关文章:

android - ViewBinding - 不同风格的布局资源

java - RecyclerView适配器类中Android View Binding的正确方式是什么?

android - BViewBinding 的缺失或冲突依赖项的模块类路径

java - 如何使用数组中存储的数据每 24 小时发送一次自动电子邮件 - Android studio

android - 选择项目时更改微调器的文本(非背景)颜色

android - 如何在样式 xml 中包含工具命名空间

android - 使用 gradle 包装器在 Kotlin 枚举类构建中的方法声明无效

android - 仅将横向模式设置为 Android Google Map Fragment

android - 为什么使用 View Binding 会改变布局?

android-layout - ViewBinding - 如何在 ViewBinding 中设置包含布局的可见性?