gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:05:40 25 4
gpt4 key购买 nike

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

val mapFragment = childFragmentManager.findFragmentById(R.id.map) asSupportMapFragment


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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com