У меня есть этот метод в моей 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, я вижу и взаимодействую с ним.три фрагмента.
Мои классы и 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
}
}
И код трехфрагменты:
Event_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
}
Event_2:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View? = inflater.inflate(R.layout.fragment_repeticiones, container, false)
return view
}
Event_3:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View? = inflater.inflate(R.layout.fragment_relaciones, container, false)
return view
}
Кто-нибудь, помогите мне, пожалуйста!Извините за мой английский