android中如何实现三个或更多fragment之间的交互?

发布于 2025-01-11 16:36:53 字数 2671 浏览 0 评论 0原文

我正在尝试将信息(latLng)从两个片段传递到第三个片段。

FirstFragment

class FirstFragment : Fragment(), OnMapReadyCallback, InitializeHelperInterface {
private lateinit var sendLatLngFromListener: OnSendLatLngListener

override fun onAttach(context: Context) {
    super.onAttach(context)
    try {
        sendLatLngFromListener = context as OnSendLatLngListener
    } catch (e: ClassCastException){
        throw ClassCastException(
            "$context должен реализовывать интерфейс OnSendLatLngFromListener")
    }
}

override fun moveCamera(latLng: LatLng, zoom: Float, title: String){

    sendLatLngFromListener.onSendLatLngFrom(latLng)

SecondFragment

private lateinit var sendLatLngToListener: OnSendLatLngListener

override fun onAttach(context: Context) {
    super.onAttach(context)

    try {
        sendLatLngToListener = context as OnSendLatLngListener
    } catch (e: ClassCastException){
        throw ClassCastException(
            "$context должен реализовывать интерфейс OnSendLatLngToListener")
    }
}

override fun moveCamera(latLng: LatLng, zoom: Float, title: String){

    sendLatLngToListener.onSendLatLngTo(latLng)

Interfaice

interface OnSendLatLngListener {

fun onSendLatLngFrom(latLngFrom: LatLng) {
}

fun onSendLatLngTo(latLngTo: LatLng){
}

}

MapsActivity

class MapsActivity : AppCompatActivity(), OnSendLatLngListener{

private lateinit var latLngFrom: LatLng
private lateinit var latLngTo: LatLng

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_maps)

    val pageAdapter: FragmentStateAdapter = TabHandler(this)
    viewpager?.adapter = pageAdapter

    val tabLayoutMediator = TabLayoutMediator(
        tabs, viewpager
    ) { tab, position ->
        when(position){
            0 -> tab.text = "Откуда"
            else -> tab.text = "Куда"
        }
    }
    tabLayoutMediator.attach()

    button()
}

override fun onSendLatLngFrom(latLngFrom: LatLng) {
    this.latLngFrom = latLngFrom
}

override fun onSendLatLngTo(latLngTo: LatLng) {
    this.latLngTo = latLngTo
}

private fun button(){
    route_button.setOnClickListener(View.OnClickListener {
        setContentView(R.layout.activity_maps_route)
        val fragment = supportFragmentManager.findFragmentById(R.id.routeFragment) as RouteFragment
        fragment.setLatLng(latLngFrom, latLngTo)
    })
}

}

但是当应用程序启动时,按钮方法不会从片段传递 LatLng。

例外

lateinit 属性 latLngTo 尚未初始化

请告诉我如何正确执行此操作。

I'm trying to pass information(latLng) from two fragments to a third one.

FirstFragment

class FirstFragment : Fragment(), OnMapReadyCallback, InitializeHelperInterface {
private lateinit var sendLatLngFromListener: OnSendLatLngListener

override fun onAttach(context: Context) {
    super.onAttach(context)
    try {
        sendLatLngFromListener = context as OnSendLatLngListener
    } catch (e: ClassCastException){
        throw ClassCastException(
            "$context должен реализовывать интерфейс OnSendLatLngFromListener")
    }
}

override fun moveCamera(latLng: LatLng, zoom: Float, title: String){

    sendLatLngFromListener.onSendLatLngFrom(latLng)

SecondFragment

private lateinit var sendLatLngToListener: OnSendLatLngListener

override fun onAttach(context: Context) {
    super.onAttach(context)

    try {
        sendLatLngToListener = context as OnSendLatLngListener
    } catch (e: ClassCastException){
        throw ClassCastException(
            "$context должен реализовывать интерфейс OnSendLatLngToListener")
    }
}

override fun moveCamera(latLng: LatLng, zoom: Float, title: String){

    sendLatLngToListener.onSendLatLngTo(latLng)

Interfaice

interface OnSendLatLngListener {

fun onSendLatLngFrom(latLngFrom: LatLng) {
}

fun onSendLatLngTo(latLngTo: LatLng){
}

}

MapsActivity

class MapsActivity : AppCompatActivity(), OnSendLatLngListener{

private lateinit var latLngFrom: LatLng
private lateinit var latLngTo: LatLng

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_maps)

    val pageAdapter: FragmentStateAdapter = TabHandler(this)
    viewpager?.adapter = pageAdapter

    val tabLayoutMediator = TabLayoutMediator(
        tabs, viewpager
    ) { tab, position ->
        when(position){
            0 -> tab.text = "Откуда"
            else -> tab.text = "Куда"
        }
    }
    tabLayoutMediator.attach()

    button()
}

override fun onSendLatLngFrom(latLngFrom: LatLng) {
    this.latLngFrom = latLngFrom
}

override fun onSendLatLngTo(latLngTo: LatLng) {
    this.latLngTo = latLngTo
}

private fun button(){
    route_button.setOnClickListener(View.OnClickListener {
        setContentView(R.layout.activity_maps_route)
        val fragment = supportFragmentManager.findFragmentById(R.id.routeFragment) as RouteFragment
        fragment.setLatLng(latLngFrom, latLngTo)
    })
}

}

But the button method is not passed LatLng from the fragments when the application starts.

Exception

lateinit property latLngTo has not been initialized

Please tell me how to do it right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叶落知秋 2025-01-18 16:36:53

您可以使用 Fragment Result API:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // Use the Kotlin extension in the fragment-ktx artifact
    setFragmentResultListener("requestKey") { requestKey, bundle ->
    // We use a String here, but any type that can be put in a Bundle is supported
    val result = bundle.getString("bundleKey")
    // Do something with the result
    }
}

button.setOnClickListener {
   val result = "result"
   // Use the Kotlin extension in the fragment-ktx artifact
   setFragmentResult("requestKey", bundleOf("bundleKey" to result))
}

有关在 Fragment 之间以及 Fragments 和 Activity 之间进行通信的方法的完整列表,请参阅文档:https://developer.android.com/guide/fragments/communicate

You can use the Fragment Result API:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // Use the Kotlin extension in the fragment-ktx artifact
    setFragmentResultListener("requestKey") { requestKey, bundle ->
    // We use a String here, but any type that can be put in a Bundle is supported
    val result = bundle.getString("bundleKey")
    // Do something with the result
    }
}

button.setOnClickListener {
   val result = "result"
   // Use the Kotlin extension in the fragment-ktx artifact
   setFragmentResult("requestKey", bundleOf("bundleKey" to result))
}

For the complete list of ways to communicate between fragments and also between fragments and activities see the doc: https://developer.android.com/guide/fragments/communicate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文