我有以下的PagerAdapter来显示我的Android片段中的标签:
class CustomTabPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
override fun getItem(position: Int): Fragment? {
var fragment: Fragment? = null
if (position == 0) {
fragment = AchievOngoingFragment()
} else if (position == 1) {
fragment = AchievDoneFragment()
}
return fragment
}
override fun getCount(): Int = 2
override fun getPageTitle(position: Int): CharSequence? {
var title: String? = null
if (position == 0) {
title = "Ongoing"
} else if (position == 1) {
title = "Done"
}
return title
}
}
我的ProfileFragment
就是这样设置的:
internal lateinit var viewpageradapter:CustomTabPagerAdapter //Declare PagerAdapter
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewpageradapter = CustomTabPagerAdapter(activity.supportFragmentManager)
viewPager.adapter = viewpageradapter //Binding PagerAdapter with ViewPager
tab_layout.setupWithViewPager(this.viewPager)
}
使用以下布局:
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="40dp"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/black"
app:tabTextColor="@color/black"
app:tabIndicatorColor="@color/colorPrimary"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_below="@id/tab_layout"/>
选项卡显示完美,但ViewPager的内容为空时,它应该是与每个选项卡关联的片段布局的内容(即AchievOngoingFragment
和AchievDoneFragment
)
我错过了什么吗?