我的MainActivity中有此方法:
private fun ObtenerFragmentos() {
var event1:Event_1? =null
var event2:Event_2? =null
var event3: Event_3? =null
for(fragment: Fragment in supportFragmentManager.fragments){
if (fragment is Event_1) event1 = fragment
if(fragment is Event_2) event2 = fragment
if(fragment is Event_3) event3 = fragment
}
}
问题在于,event3始终为null,supportFragmentManager.fragments.size始终获得2个片段,但是当我运行apk时,我可以看到这3个片段并与之交互。
我的类和xml:
activity_main3.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme1" >
<androidx.viewpager.widget.ViewPager
android:id="@+id/mViewPager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar_event"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="@color/colorblanco"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/colorblanco" />
</androidx.viewpager.widget.ViewPager>
</RelativeLayout>
MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main3)
viewpageradapter = ViewPageAdapter(supportFragmentManager)
mViewPager.adapter = viewpageradapter
tab_layout.setupWithViewPager(mViewPager)
tab_layout.getTabAt(0)!!.setIcon(R.drawable.ic_datos_generales)
tab_layout.getTabAt(1)!!.setIcon(R.drawable.ic_repeat)
tab_layout.getTabAt(2)!!.setIcon(R.drawable.ic_relaciones)
}
这是ViewPageAdapter类:
class ViewPageAdapter(fm: androidx.fragment.app.FragmentManager) : androidx.fragment.app.FragmentPagerAdapter(fm) {
override fun getItem(position: Int): androidx.fragment.app.Fragment {
var fragment: androidx.fragment.app.Fragment? = null
when (position) {
0 -> fragment = Event_1()
1 -> fragment = Event_2()
2 -> fragment = Event_3()
}
return fragment!!
}
override fun getCount(): Int {
return 3
}
override fun getPageTitle(position: Int): CharSequence? {
var title: String? = null
if (position == 0) {
title = "Datos Generales"
} else if (position == 1) {
title = "Repeticiones"
} else if (position == 2) {
title = "Relaciones"
}
return title
}
}
这三个片段的代码是:
事件_1:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val viewo: View? = inflater.inflate(R.layout.fragment_datos_generales, container, false)
return viewo
}
事件_2:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View? = inflater.inflate(R.layout.fragment_repeticiones, container, false)
return view
}
事件3:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View? = inflater.inflate(R.layout.fragment_relaciones, container, false)
return view
}
请有人帮我!!对不起,我的英语
答案 0 :(得分:2)
您可以使片段在适配器内保持静态,然后直接从主活动中访问它们,而无需尝试从片段管理器中提取它们。
例如,如下更改适配器的顶部。
class ViewPageAdapter(fm: androidx.fragment.app.FragmentManager) : androidx.fragment.app.FragmentPagerAdapter(fm) {
public var event1 = Event_1()
public var event2 = Event_2()
public var event3 = Event_3()
override fun getItem(position: Int): androidx.fragment.app.Fragment? {
return when (position) {
0 -> event1
1 -> event2
2 -> event3
else -> null
}
}
...
然后您可以在主要活动中使用(例如)
viewpageradapter.event1