MagicIndicator的Item默认是match_parent的。所以需要自定义CommonPagerTitleView
var commonNavigator = CommonNavigator(this)
commonNavigator.adapter = object : CommonNavigatorAdapter() {
override fun getCount() = stringList.size
override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
var commonTextView = CommonPagerTitleView(context)
var bn_textView = LayoutInflater.from(context).inflate(R.layout.bn_up_textview,null) as TextView
bn_textView.text = stringList[index]
commonTextView.setContentView(bn_textView)
var fp = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT)
fp.gravity = Gravity.CENTER_VERTICAL
fp.setMargins(UIUtil.dip2px(context,5.0),0,0,0)
bn_textView.layoutParams = fp
commonTextView.onPagerTitleChangeListener = object:OnPagerTitleChangeListener{
override fun onSelected(index: Int, totalCount: Int) {
bn_textView.setTextColor(Color.parseColor("#FFFFFF"))
bn_textView.setBackgroundResource(R.drawable.bn_dowm_catetories_selected)
}
override fun onDeselected(index: Int, totalCount: Int) {
bn_textView.setTextColor(Color.parseColor("#66000000"))
bn_textView.setBackgroundResource(R.drawable.bn_dowm_catetories_unselected)
}
override fun onLeave(
index: Int,
totalCount: Int,
leavePercent: Float,
leftToRight: Boolean
) {
}
override fun onEnter(
index: Int,
totalCount: Int,
enterPercent: Float,
leftToRight: Boolean
) {
}
}
commonTextView.setOnClickListener {
mDataBind.miSelectUp.onPageSelected(index)
mDataBind.miSelectUp.onPageScrolled(index, 0f, 0)
}
return commonTextView
}
override fun getIndicator(context: Context?): IPagerIndicator? {
return null
}
}
mDataBind.miSelectUp.navigator = commonNavigator
看下效果