android中如何实现三个或更多fragment之间的交互?
我正在尝试将信息(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Fragment Result API:
有关在 Fragment 之间以及 Fragments 和 Activity 之间进行通信的方法的完整列表,请参阅文档:https://developer.android.com/guide/fragments/communicate
You can use the Fragment Result API:
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