private var titles= mutableListOf<String>()
private var icons= mutableListOf<Int>()
val stringArray = resources.getStringArray(R.array.main_title_name)
titles.addAll(stringArray)
icons.add(R.drawable.ic_tab_home_selector)
icons.add(R.drawable.ic_tab_img_selector)
icons.add(R.drawable.ic_tab_me_selector)
var commonNavigator:CommonNavigator= CommonNavigator(mContext)
commonNavigator.isAdjustMode=true
var commonNavigatorAdapter:CommonNavigatorAdapter=object:CommonNavigatorAdapter(){
override fun getCount(): Int {
return titles.size!!
}
override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
var commonPagerTitleView:CommonPagerTitleView=CommonPagerTitleView(mContext)
// View customLayout
var customLayout:View=LayoutInflater.from(mContext).inflate(R.layout.tab_home_title,null)
val img = customLayout.findViewById<ImageView>(R.id.item_title_img)
val tv = customLayout.findViewById<TextView>(R.id.item_title_text)
img.setImageResource(icons?.get(index)!!)
tv.setText(titles?.get(index))
commonPagerTitleView.setContentView(customLayout)
commonPagerTitleView.onPagerTitleChangeListener=object :OnPagerTitleChangeListener{
override fun onSelected(index: Int, totalCount: Int) {
img.isSelected=true
tv.isSelected=true
}
override fun onDeselected(index: Int, totalCount: Int) {
img.isSelected=false
tv.isSelected=false
}
override fun onLeave(
index: Int,
totalCount: Int,
leavePercent: Float,
leftToRight: Boolean
) {
}
override fun onEnter(
index: Int,
totalCount: Int,
enterPercent: Float,
leftToRight: Boolean
) {
}
}
commonPagerTitleView.setOnClickListener {
if (index!=viewPager?.currentItem){
viewPager?.setCurrentItem(index,false)
}
}
return commonPagerTitleView
}
override fun getIndicator(context: Context?): IPagerIndicator? {
return null
}
}
viewPager?.setOnPageChangeListener(object :ViewPager.OnPageChangeListener{
override fun onPageScrolled(
position: Int,
positionOffset: Float,
positionOffsetPixels: Int
) {
}
override fun onPageSelected(position: Int) {
}
override fun onPageScrollStateChanged(state: Int) {
}
})
commonNavigator.adapter=commonNavigatorAdapter
indicator?.navigator=commonNavigator
ViewPagerHelper.bind(indicator,viewPager)
viewPager?.setCurrentItem(0)
kotlin magicindicator
最新推荐文章于 2023-04-15 00:51:06 发布
本文展示了如何在Android中创建一个可自定义的底部导航栏,包括设置标题、图标,并实现点击监听。通过实例代码详细解释了如何使用CommonNavigator和ViewPager进行页面切换,并实现了标题选中和未选中的状态切换效果。
摘要由CSDN通过智能技术生成