【问题标题】:getPageTitle() not getting called by Tablayout inside fragmentgetPageTitle() 没有被片段内的 Tablayout 调用
【发布时间】:2023-03-22 13:12:01
【问题描述】:

我刚刚将 Android Studio 模板 Tab 布局从活动复制到片段。我能够显示和滑动片段,但不幸的是,getPageTitle() 没有被调用,标签上的标题仍然是空的。代码如下。您能否查看并帮助我解决此问题?

TabLayout 片段

class CamTabs : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view =  inflater.inflate(R.layout.fragment_cam_tabs, container, false)
       val adapter = SectionsPagerAdapter(this.context!!,this.childFragmentManager!!)

        view.viewPager.adapter = adapter
        view.tabs.setupWithViewPager(viewPager)


        return view
    }

}

标签布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"``
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary" />[![enter image description here][1]][1]
    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

页面适配器

class SectionsPagerAdapter(private val context: Context, fm: FragmentManager)
    : FragmentPagerAdapter(fm) {

    override fun getItem(position: Int): Fragment {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        return PlaceholderFragment.newInstance(position + 1)
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return context.resources.getString(TAB_TITLES[position])
    }

    override fun getCount(): Int {
        // Show 2 total pages.
        return 2
    }
}

占位符片段

class PlaceholderFragment : Fragment() {

    private lateinit var pageViewModel: PageViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        pageViewModel = ViewModelProviders.of(this).get(PageViewModel::class.java).apply {
            setIndex(arguments?.getInt(ARG_SECTION_NUMBER) ?: 1)
        }
    }

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_main, container, false)
        val textView: TextView = root.findViewById(R.id.section_label)
        pageViewModel.text.observe(this, Observer<String> {
            textView.text = it
        })
        return root
    }

    companion object {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private const val ARG_SECTION_NUMBER = "section_number"

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        @JvmStatic
        fun newInstance(sectionNumber: Int): PlaceholderFragment {
            return PlaceholderFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_SECTION_NUMBER, sectionNumber)
                }
            }`enter code here`
        }
    }
}

片段图片显示在这里:

【问题讨论】:

  • 你如何确定getPageTitle() 没有被调用?您是否在此处放置了断点或日志打印?你确定你只是看不到标题吗?因为你已经将tabTextColor 设置为白色,而且背景也是白色的。
  • 是的,我尝试将断点放在 getpagetitle() 中,但它不起作用。当我尝试使用不同的背景时也没有显示。

标签: android android-fragments kotlin android-tabs


【解决方案1】:

将代码移到方法中解决:

class CamTabs : Fragment() {

覆盖 fun onViewCreated(view: View, savedInstanceState: Bundle?) {

    view.view_pager.adapter = SectionsPagerAdapter(this.context!!, childFragmentManager)
     view.tabs.setupWithViewPager(view.view_pager,true)

}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-08
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2018-10-01
    • 1970-01-01
    相关资源
    最近更新 更多