magicIndicator使用简记

转载请注明出处:
http://blog.csdn.net/user11223344abc?viewmode=contents
出自【蛟-blog】

项目地址:
https://github.com/hackware1993/MagicIndicator

本文实现的需求为:
顶部4个tab,下方拖4个Viewpager+Fragment。

核心初始化代码:

public class ZJTestActivity extends AppCompatActivity {

    private MagicIndicator mMagicIndicator = null;
    private static final String[] CHANNELS = new String[]{"全部", "待付款", "待发货","待收货"};
    private List mDataList = Arrays.asList(CHANNELS);
    private ExamplePagerAdapter mExamplePagerAdapter = new ExamplePagerAdapter(mDataList);
    private ViewPager mViewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zjtest);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mViewPager.setAdapter(mExamplePagerAdapter);
        initMagicIndicator4();
    }

    private void initMagicIndicator4() {
        MagicIndicator magicIndicator = (MagicIndicator) findViewById(R.id.magic_indicator4);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                BadgePagerTitleView badgePagerTitleView = new BadgePagerTitleView(context);

                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);//设置为可渐变的View
                simplePagerTitleView.setNormalColor(Color.GRAY);//字渐变前
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.colorPrimarys));//字渐变后
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setTextSize(15);//设置字体大小
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                badgePagerTitleView.setInnerPagerTitleView(simplePagerTitleView);

                return badgePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setColors(getResources().getColor(R.color.colorPrimarys));//线的颜色
                linePagerIndicator.setMode(2);
                linePagerIndicator.setLineHeight(UIUtil.dip2px(ZJTestActivity.this,3));
                linePagerIndicator.setLineWidth(UIUtil.dip2px(ZJTestActivity.this,10));
                return linePagerIndicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE); //设置tab的显示模式,SHOW_DIVIDER_MIDDLE为居中
        titleContainer.setDividerDrawable(new ColorDrawable() {
            @Override
            public int getIntrinsicWidth() {
                return UIUtil.dip2px(ZJTestActivity.this, 30);//tab的宽度
            }
        });
        ViewPagerHelper.bind(magicIndicator, mViewPager);
    }
}

本文Demo:
点击下载
对应项目为magicIndicator

你可能感兴趣的:(android,基础知识点,android,自定义view)