问题描述
我对android开发非常陌生。我正在开发一个带有标签,底部导航视图和片段的示例应用程序。我写了一些代码,并可能产生一些输出。标签页正确可见,我可以在标签页之间导航。但是不会调用各个片段,因此,所有选项卡始终为空。有人可以帮我吗?
这是我创建的适配器类
class SectionsPagerAdapter(
private val fragmentActivity: FragmentActivity
) : FragmentStateAdapter(fragmentActivity) {
override fun getItemCount(): Int {
Log.d(TAG,"getItemCount: ")
return 3
}
override fun createFragment(position: Int): Fragment {
Log.d(TAG,"createFragment: $position")
return when(position) {
0 -> CameraFragment()
2 -> MessagesFragment()
else -> HomeFragment()
}
}
companion object {
private const val TAG = "SectionsPagerAdapter"
}
}
MainActivity的onCreate具有用于设置适配器的功能
private fun setupViewPager() {
Log.d(TAG,"setupViewPager: ")
val adapter = SectionsPagerAdapter(this)
centerScreenViewPager.adapter = adapter
centerScreenViewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL
TabLayoutMediator(tabs,centerScreenViewPager) {tab,position ->
tab.text = "Tab $position"
}.attach()
}
这是片段创建的xml之一(所有其他片段的代码相似,但背景颜色不同)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/cameraFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000FF"
/>
viewpager2像这样合并创建
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/centerScreenViewPager"
android:layout_width="0dp"
android:layout_height="0dp"
android:layoutDirection="rtl"
/>
</merge>
我已经将它包含在activity_main.xml
中
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/viewPagerContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".home.MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/topHeaderLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<include
layout="@layout/layout_top_tabs"
android:layout_width="0dp"
android:layout_height="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/menu_bottom_navigation" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/viewPagerConstraintLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/topHeaderLayout">
<include
layout="@layout/layout_center_viewpager"
android:layout_width="0dp"
android:layout_height="0dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是片段类之一(所有其他类似)
class CameraFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
): View? {
Log.d(TAG,"onCreateView:")
return inflater.inflate(R.layout.fragment_camera,container,false)
}
companion object {
private const val TAG = "CameraFragment"
}
}
当我在Logcat中检查SectionsPagerAdapter
时,发现根本没有调用createFragment
。但是getItemCount
被多次呼叫。
我在这里做错什么了吗?有人可以帮我吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)