我们项目里用了com.github.hackware1993:MagicIndicator

        1个简单的需求,tab选中时字体放大,颜色变化,并加粗

        但是却遇到一个bug,那就是默认选中的第一个tab里面的文字被省略号了。

        解决:其实很简单,就是把getTitleView中的字号大小写成选中时放大的字号。

  private CommonNavigator createNavigator(ArrayList<String> titleList, ViewPager2 viewPager, List<TopMenuEntity> entities) {
        CommonNavigator commonNavigator = new CommonNavigator(requireContext());
        CommonNavigatorAdapter navigatorAdapter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titleList == null ? 0 : titleList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, int index) {
                ColorTransitionPagerTitleView titleView = new         
                                 ColorTransitionPagerTitleView(context);
                titleView.setText(titleList.get(index));
             //   就是这里******************
              //  titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                titleView. setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
                titleView.setNormalColor(Color.parseColor("#99FFFFFF"));
                titleView.setSelectedColor(Color.parseColor("#FFFFFF"));
                titleView.setOnClickListener(view -> {
                    viewPager.setCurrentItem(index);
                    TopMenuEntity entity = entities.get(index);
                    mClickType = entity.getClickType();
                    mSeletedTabPosition = index;
                });
                return titleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setYOffset(UIUtil.dip2px(context, 6));
                indicator.setLineHeight(UIUtil.dip2px(context, 3));
                indicator.setLineWidth(UIUtil.dip2px(context, 16));
                AccelerateInterpolator startInterpolator = new AccelerateInterpolator();
                DecelerateInterpolator endInterpolator = new 
                            DecelerateInterpolator(2.0f);
                indicator.setStartInterpolator(startInterpolator);
                indicator.setEndInterpolator(endInterpolator);
                indicator.setShaderColors(Color.parseColor("#FFFFFF"), 
                Color.parseColor("#FFFFFF"));
                indicator.setShaderPos(0.0f, 0.0f);
                indicator.setRoundRadius(UIUtil.dip2px(context, 1.0));
                return indicator;
            }
        };
        commonNavigator.setAdapter(navigatorAdapter);
        return commonNavigator;
    }
public class ColorTransitionPagerTitleView extends SimplePagerTitleView {

    public ColorTransitionPagerTitleView(Context context) {
        super(context);
    }

    @Override
    public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {
        int color = ArgbEvaluatorHolder.eval(leavePercent, mSelectedColor, mNormalColor);
        setTextColor(color);
        getPaint().setFakeBoldText(false);
        setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
    }

    @Override
    public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {
        int color = ArgbEvaluatorHolder.eval(enterPercent, mNormalColor, mSelectedColor);
        setTextColor(color);
        getPaint().setFakeBoldText(true);
        setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
    }

    @Override
    public void onSelected(int index, int totalCount) {
    }

    @Override
    public void onDeselected(int index, int totalCount) {
    }
......

这样未选中的字体其实也会变成小一些的字号

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐