kotlin中TabLayout和ViewPager2自定义tabview

文章目录

    • xml
    • tabview自定义item布局(tabIndicator底部指示器)
    • 动态tab数据
    • ViewPager2适配器FragmentStateAdapter
    • UI调用

xml

        
        
        

tabview自定义item布局(tabIndicator底部指示器)



    

    
        
        
    

    

动态tab数据

class DataGenerator {
    companion object{
        private val textList = arrayOf("提醒","私信")
        fun getTabView(context: Context, position:Int) : View {
            val view: View =
                LayoutInflater.from(context).inflate(R.layout.re_tab_item_layout, null)
            var text1 = view.findViewById(R.id.text1)
            var text_tips = view.findViewById(R.id.text_tips)
            text1.text = textList[position]
            return view
        }
    }
}

ViewPager2适配器FragmentStateAdapter

添加adapter

class ReFragmentMsgAdapter(fragmentManager: FragmentManager,lifecycle:Lifecycle):FragmentStateAdapter(fragmentManager,lifecycle) {

    private val mFragments = mutableListOf()
    override fun getItemCount(): Int = mFragments.size
    override fun createFragment(position: Int): Fragment = mFragments[position]

    fun add(fragment:Fragment){
        mFragments.add(fragment)
    }
}

UI调用

  fragmentAdapter = ReFragmentMsgAdapter(supportFragmentManager,lifecycle)
        fragmentAdapter.add(ReTipsFragment())
        fragmentAdapter.add(ReMsgFragment())
        viewPager2Msg.adapter = fragmentAdapter
        val tabLayoutMediator = TabLayoutMediator(tab_layout_tips,viewPager2Msg) {
                tab, position ->
            if(position == 0) {
                tab.customView = DataGenerator.getTabView(this,0)
                tab.customView?.findViewById(R.id.iv_indicator)?.visibility = View.VISIBLE
                tab.customView?.findViewById(R.id.rl_tips)?.visibility = View.VISIBLE
                tab.customView?.findViewById(R.id.text1)?.setTextColor(Color.BLACK)
            }else{
                tab.customView = DataGenerator.getTabView(this,1)
                tab.customView?.findViewById(R.id.iv_indicator)?.visibility = View.GONE
                tab.customView?.findViewById(R.id.rl_tips)?.visibility = View.GONE
                tab.customView?.findViewById(R.id.text1)?.setTextColor(Color.parseColor("#333333"))
            }
        }
        recoverItem()
        tabLayoutMediator.attach()
        tab_layout_tips.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                recoverItem()
                chooseTab(tab)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }
        })

private fun recoverItem() {
        for (i in 0..1){
            var ivIndicator = tab_layout_tips?.getTabAt(i)?.view?.findViewById(R.id.iv_indicator)
            var text1 = tab_layout_tips?.getTabAt(i)?.view?.findViewById(R.id.text1)
            var rlTips = tab_layout_tips?.getTabAt(i)?.view?.findViewById(R.id.rl_tips)
            rlTips?.visibility = View.GONE
            ivIndicator?.visibility = View.GONE
            text1?.setTextColor(Color.parseColor("#333333"))
        }
    }

    private fun chooseTab(tab: TabLayout.Tab?) {
            var ivIndicator = tab?.view?.findViewById(R.id.iv_indicator)
            var text1 = tab?.view?.findViewById(R.id.text1)
            var rlTips = tab?.view?.findViewById(R.id.rl_tips)
            var textTips = tab?.view?.findViewById(R.id.text_tips)
            rlTips?.visibility = View.VISIBLE
            ivIndicator?.visibility = View.VISIBLE
            text1?.setTextColor(Color.BLACK)
    }

你可能感兴趣的:(Kotlin,tablayout用法,ViewPager2,Fragment,ViewPager2适配器)