gpt4 book ai didi

android - 通过适配器在 fragment 之间传递数据 (Kotlin Android)

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

刚接触编码 ~1 个月。如果在其他地方回答了这个问题,我们深表歉意,但花了几个小时浏览其他类似的问题,但不能完全正确。 What: 尝试将位置点击数据从 fragment 1 (MainFragment) 传递到 fragment 2 (MapsFragment) 以加载相应的 map 。通过适配器 onclick 方法,因为我想在 MainFragment 上使用 RecyclerView。项目是可构建的,但当我单击 recyclerview 上的项目时崩溃(无法打开 map )。怎么了????

提前致谢!!

编辑:已回答谢谢大家的评论。能够通过以下方式解决此问题:1) 将 mapsfragment 上的 lateinit var 更改为常规 var + nullable,以及 2) 将主 fragment 中的 bundle 行更改为

val bundle = bundleOf(EXTRA_USER_MAP to sports[position])
view.findNavController(.navigate(R.id.action_mainFragment_to_mapsFragment, bundle)

哇哦 Kotlin !

主要 fragment :

const val EXTRA_USER_MAP = "EXTRA_USER_MAP"
private const val TAG = "MainFragment"

class MainFragment : Fragment() {

override fun onCreateView ... // code to inflate my fragment view

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val sports = generateData()

myMaps.layoutManager = LinearLayoutManager(activity)

myMaps.adapter = this.context?.let {
MapsAdapter(it, sports, object: MapsAdapter.OnClickListener {
override fun onItemClick(position: Int) {
Log.i(TAG, "onItemClick $position")

// When user taps on view in recyclerview, navigate to new fragment and opens corresponding map
val bundle = Bundle()
bundle.putSerializable(EXTRA_USER_MAP, sports[position])

view.findNavController().navigate(R.id.action_mainFragment_to_mapsFragment)
}
})
}
}
private fun generateData(): List<Sport> { ... // code for my data set

map fragment

private const val TAG = "MapsFragment"
class MapsFragment : Fragment(), OnMapReadyCallback {

private lateinit var map: GoogleMap
private lateinit var sport: Sport

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_maps, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val bundle = arguments
if (bundle !=null) {
sport = bundle.getSerializable(EXTRA_USER_MAP) as Sport
}

val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
}

// Actions for map
override fun onMapReady(googleMap: GoogleMap) {
map = googleMap
Log.i(TAG, "Map rendered: ${sport.title}") // Logcat check once working
...

相关适配器代码

class MapsAdapter(
val context: Context,
val sports: List<Sport>,
val onClickListener: OnClickListener)
: RecyclerView.Adapter<MapsAdapter.ViewHolder>() {

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

interface OnClickListener {
fun onItemClick(position: Int)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val sport = sports[position]
holder.itemView.setOnClickListener {
Log.i(TAG, "Tapped on position $position")
onClickListener.onItemClick(position)

}
...

最佳答案

您可以拥有一个共享 View 模型,其中您可以拥有一个 LiveData 属性,您可以从 MainFragment 更改他的值并从 MapsFragment 监听他的更改并做任何您想做的事情,或者您可以有一个监听器并可以在用户点击和监听时监听得到位置。希望对你有所帮助

关于android - 通过适配器在 fragment 之间传递数据 (Kotlin Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65403150/

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