选项卡布局不显示选项卡标题

时间:2019-10-07 16:52:41

标签: android android-fragments kotlin android-tablayout

我正在尝试以编程方式设置选项卡布局的名称。我尝试了所有可能的方法,但我想我缺少了一些东西。这是我的适配器,片段和XML布局的代码。我也添加了一些评论部分。请建议是否可以使用它。如果可能,请在kotlin中建议代码

class EventsFragment : Fragment(), ViewPager.OnPageChangeListener {

    private var eventDayStatusTabAdapter: EventDayStatusTabAdapter? = null
    protected lateinit var mActivity: AppCompatActivity


    companion object {
        fun newInstance(): EventsFragment =
            EventsFragment()
    }


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

        mActivity = activity as AppCompatActivity

        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        doInitialSetup()
    }

    private fun doInitialSetup() {
        setUpLoanStatusTabAdapter()
    }


    private fun setUpLoanStatusTabAdapter() {
        val subTab = arguments?.getString("extraSubSection")
        val activeTabPosition = when (subTab) {
            "onGoing" -> 0
            "day1" -> 1
            "day2" -> 2
            "day"  -> 3
             else->   0
        }

        eventDayStatusTabAdapter = EventDayStatusTabAdapter(
            childFragmentManager, activeTabPosition
        )
        eventsViewPager.adapter = eventDayStatusTabAdapter

        //this is must for viewpager to work and enabling this will take out NAMES that you declared in xml
        eventsTabLayout.setupWithViewPager(eventsViewPager)
//        eventsTabLayout.removeAllTabs()

//        eventDayStatusTabAdapter?.let {
//            for (i in 0 until it.count) {
//                val tab = eventsTabLayout.newTab()
//                val tabName =
//                    mActivity.layoutInflater.inflate(R.layout.events_tab, eventsTabLayout, false) as TextView
//                tabName.text = resources.getStringArray(R.array.eventsDayName)[i]
//                tab.customView = tabName
//                eventsTabLayout.addTab(tab, i, 0 == i)
//                tabName.isSelected = 4 == i
//                if (0 == i) {
//                    tab.select()
//                }
//            }
//        }

        eventsViewPager.addOnPageChangeListener(this)
        eventsTabLayout.getTabAt(activeTabPosition)?.select()

    }

    override fun onPageScrollStateChanged(state: Int) {
        //Do nothing
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        //Do nothing
    }

    override fun onPageSelected(position: Int) {
        //Do nothing
    }

}





class EventDayStatusTabAdapter(efm: FragmentManager?, private var activePosition: Int?) :
    FragmentPagerAdapter(efm) {

    private val TAG = javaClass.name

    private var onGoing: OnGoing? = null
    private var day1: Day1? = null
    private var day2: Day2? = null
    private var day3: Day3? = null


    private val TAB_COUNT = 4

    private val tabTitles = arrayOf("ONGOING", "DAY 1", "DAY 2", "DAY 3")

    override fun getItem(position: Int): Fragment {
        when (position) {
            0 -> {
                return OnGoing.newInstance()
            }
            1 -> {
                return Day1.newInstance()
            }
            2 -> {
                return Day2.newInstance()
            }
            3 -> {
                return Day3.newInstance()
            }
            else -> {
                return OnGoing.newInstance()
            }
        }
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val fragment = super.instantiateItem(container, position)

        when (position) {
            0 -> {
                if (fragment is OnGoing) {
                    onGoing = OnGoing()
                }
            }
            1 -> {
                if (fragment is Day1) {
                    day1 = Day1()
                }
            }
            2 -> {
                if (fragment is Day2) {
                    day2 = Day2()
                }
            }
            3 -> {
                if (fragment is Day3) {
                    day3 = Day3()
                }
            }
            else -> {
                if (fragment is OnGoing) {
                    onGoing = OnGoing()
                }
            }
        }

        return fragment
    }

    override fun getCount(): Int {
        return TAB_COUNT
    }


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




<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/eventsBackgroundColor"
        android:orientation="vertical">


    <com.google.android.material.tabs.TabLayout
            android:id="@+id/eventsTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabBackground="@drawable/tab_color_selector"
            app:tabGravity="fill"
            app:tabPaddingTop="76dp"
            app:tabTextColor="@android:color/background_light" />

<!--        app:tabIndicatorColor="@color/whiteText"-->
<!--        app:tabIndicatorHeight="3dp"-->

<!--        <com.google.android.material.tabs.TabItem-->
<!--                android:layout_width="wrap_content"-->
<!--                android:layout_height="wrap_content"-->
<!--                android:text="ONGOING" />-->



    <androidx.viewpager.widget.ViewPager
            android:id="@+id/eventsViewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
</LinearLayout>

0 个答案:

没有答案