Change image on Tab in TabLayout when Selected

后端 未结 3 1172
北荒
北荒 2021-01-05 02:11

I am using Design TabLayout,



        
3条回答
  •  孤街浪徒
    2021-01-05 03:09

    As setOnTabSelectedListener is deprecated, and there is no new answer, I'll update an updated answer

    Should use addOnTabSelectedListener with OnTabSelectedListener

       tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabReselected(p0: TabLayout.Tab?) {
                }
    
                override fun onTabUnselected(p0: TabLayout.Tab?) {
                    (p0?.customView?.findViewById(R.id.tab) as TextView).setTextColor(
                        context!!.resources.getColor(
                            R.color.v3_tint_tab_unselected_color
                        )
                    )
                }
    
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    (tab?.customView?.findViewById(R.id.tab) as TextView).setTextColor(
                        context!!.resources.getColor(
                            R.color.v3_tint_tab_selected_color
                        )
         (tab?.customView?.findViewById(R.id.tab_img) as ImageView).setImageResource(R.drawable.custom_image)
    
        //for getting the posiiton if needed you can use
          tab.position or tab.getPosition()
                    )
                }
            })
    

    Custom tab xml

    
    
    
    
    
    
    
    

提交回复
热议问题