MagicIndicator不与ViewPager连用的用法

   private fun initMagicIndicator() {
        var fragmentContainerHelper = FragmentContainerHelper()
        fragmentContainerHelper.handlePageSelected(0)
        var typeList = resources.getStringArray(R.array.home_currency_type).toList()
        var commonNavigator = CommonNavigator(activity)
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount() = typeList.size

            override fun getTitleView(context: Context?, index: Int): IPagerTitleView {
                var textView = SimplePagerTitleView(context)

                context?.let {
                    textView.selectedColor =
                        ContextCompat.getColor(it, com.xinfa.base.R.color.base_term_2)
                    textView.normalColor =
                        ContextCompat.getColor(it, com.xinfa.base.R.color.base_country_color)
                }
                textView.text = typeList[index]
                textView.setOnClickListener {
                    fragmentContainerHelper.handlePageSelected(index)
                    
                }
                return textView
            }

            override fun getIndicator(context: Context?): IPagerIndicator {
                var indicator = NoViewPagerLinePagerIndicator(context)
                indicator.apply {
                    mode = MODE_EXACTLY
                    lineHeight = DisplayMetricsUtil.dip2px(context, 2.0F).toFloat()
                    lineWidth = DisplayMetricsUtil.dip2px(context, 16.0F).toFloat()
                    roundRadius = DisplayMetricsUtil.dip2px(context, 2.0F).toFloat()
                }
                return indicator
            }
        }
        viewBinding.magicIndicator.navigator = commonNavigator
        fragmentContainerHelper.attachMagicIndicator(viewBinding.magicIndicator)
    }
package com.xinfa.base.views;

import android.content.Context;

import androidx.core.content.ContextCompat;

import com.xinfa.base.R;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

public class NoViewPagerLinePagerIndicator extends LinePagerIndicator {
    private Context context;
    public NoViewPagerLinePagerIndicator(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public void onPageSelected(int position) {
        super.onPageSelected(position);
        getPaint().setColor(ContextCompat.getColor(context,R.color.base_term_2));
    }
}

你可能感兴趣的:(随笔,android)