选项卡未添加到页面的问题

时间:2018-11-26 18:03:50

标签: android kotlin


我正在创建一个项目,我需要在我的应用程序的首页上显示5个选项卡并进行导航。
我从这两个链接中遵循了本教程,并以我的方式https://medium.com/@eijaz/getting-started-with-tablayout-in-android-kotlin-bb7e21783761https://www.youtube.com/watch?v=bNpWGI_hGGg&t=68s进行了修改(使它适应Kotlin),但是当我编译并运行它时,它只是显示屏幕没有选项卡,如空白页。我该如何进行?我真的找不到错误。


这是我使用的适配器


class SectionsAdapter(fm: FragmentManager?) : FragmentPagerAdapter(fm) {
    private val listOfTabs = mutableListOf<Fragment>()

    private val listOfTabsTitle = mutableListOf<String>()

    fun addFragment(fragment: Fragment, title: String ) {
        listOfTabs.add(fragment)
        listOfTabsTitle.add(title)
    }

    override fun getPageTitle(position: Int): CharSequence {
        return listOfTabsTitle[position]
    }

    override fun getItem(position: Int): Fragment? {
        return listOfTabs[position]
    }

    override fun getCount(): Int {
        return listOfTabs.size
    }


这是我上课的主要内容


private fun setupSectionsAdapter() {
        var adapter = SectionsAdapter(supportFragmentManager)

        with(adapter) {
            addFragment(Tab1Fragment(), "Tab1")
            addFragment(Tab2Fragment(), "Tab2")
            addFragment(Tab3Fragment(), "Tab3")
            addFragment(Tab4Fragment(), "Tab4")
            addFragment(Tab5Fragment(), "Tab5")
        }

        container.adapter = adapter
        tabs.setupWithViewPager(container as ViewPager)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setupSectionsAdapter()
        container.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tabs))
        tabs.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(container))

        setContentView(R.layout.activity_list_of_items)    
    }


片段类


override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater?.inflate(R.layout.fragment_tab1, container, false)

        return view
    }

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

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        fun newInstance(sectionNumber: Int): Tab1Fragment {
            val fragment = Tab1Fragment()
            val args = Bundle()
            args.putInt(ARG_SECTION_NUMBER, sectionNumber)
            fragment.arguments = args
            return fragment
        }
    }


这是片段的XML


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="AJASDAJSD"/>
</android.support.constraint.ConstraintLayout>


这是我要实现标签的页面的XML


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ListOfItems">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.constraint.ConstraintLayout>

你知道我在想什么吗?前期,很抱歉,如果它翻倍,但是我在这里找不到与我的问题类似的东西。 谢谢!

0 个答案:

没有答案