Android-指示器-MagicIndicator的使用

1崔拥、要導(dǎo)入代碼或引用包

compile project(':magicindicator')
repositories {
    ...
    maven {
        url "https://jitpack.io"
    }
}

dependencies {
    ...
    compile 'com.github.hackware1993:MagicIndicator:1.5.0'
}

2蛾绎、如果使用指示器和ViewPage結(jié)合使用,則先建一個(gè)枚舉保存頻道

public enum CHANNEL {

  MY("我的", 0x01),

  DISCORY("發(fā)現(xiàn)", 0x02),

  FRIEND("朋友", 0x03),

  VIDEO("視頻", 0x04);

  //所有類型標(biāo)識(shí)
  public static final int MINE_ID = 0x01;
  public static final int DISCORY_ID = 0x02;
  public static final int FRIEND_ID = 0x03;
  public static final int VIDEO_ID = 0x04;

  private final String key;
  private final int value;

  CHANNEL(String key, int value) {
    this.key = key;
    this.value = value;
  }

  public int getValue() {
    return value;
  }

  public String getKey() {
    return key;
  }
}

3辕羽、使用數(shù)組保存頻道值

    private static final CHANNEL[] CHANNELS =
            new CHANNEL[]{CHANNEL.MY, CHANNEL.DISCORY, CHANNEL.FRIEND};

4、添加帶顏色漸變和縮放的指示器標(biāo)題的ScaleTransitionPagerTitleView
ScaleTransitionPagerTitleView.java

import android.content.Context;

import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

/**
 * 帶顏色漸變和縮放的指示器標(biāo)題
 * 博客: http://hackware.lucode.net
 * Created by hackware on 2016/6/26.
 */
public class ScaleTransitionPagerTitleView extends ColorTransitionPagerTitleView {
    private float mMinScale = 0.9f;

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

    @Override
    public void onEnter(int index, int totalCount, float enterPercent, boolean leftToRight) {
        super.onEnter(index, totalCount, enterPercent, leftToRight);    // 實(shí)現(xiàn)顏色漸變
        setScaleX(mMinScale + (1.0f - mMinScale) * enterPercent);
        setScaleY(mMinScale + (1.0f - mMinScale) * enterPercent);
    }

    @Override
    public void onLeave(int index, int totalCount, float leavePercent, boolean leftToRight) {
        super.onLeave(index, totalCount, leavePercent, leftToRight);    // 實(shí)現(xiàn)顏色漸變
        setScaleX(1.0f + (mMinScale - 1.0f) * leavePercent);
        setScaleY(1.0f + (mMinScale - 1.0f) * leavePercent);
    }

    public float getMinScale() {
        return mMinScale;
    }

    public void setMinScale(float minScale) {
        mMinScale = minScale;
    }
}

5罪佳、初始化指示器

initMagicIndicator();
    private void initMagicIndicator() {
        MagicIndicator magicIndicator = findViewById(R.id.magic_indicator);
        magicIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return CHANNELS == null ? 0 : CHANNELS.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(CHANNELS[index].getKey());
                simplePagerTitleView.setTextSize(19);
                simplePagerTitleView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                simplePagerTitleView.setNormalColor(Color.parseColor("#999999"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#333333"));
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, mViewPager);
    }

6逛漫、編寫ViewPage適配器
繼承FragmentPagerAdapter 還是FragmentStatePagerAdapter
應(yīng)先考慮頁(yè)面數(shù)是否超過3頁(yè),超過用后者赘艳。

/**
 * 首頁(yè)ViewPage適配器
 */
public class HomePagerAdapter extends FragmentPagerAdapter {

    private CHANNEL[] mList;

    public HomePagerAdapter(FragmentManager fm, CHANNEL[] datas) {
        super(fm);
        mList = datas;
    }

    //這種方式,避免一次性創(chuàng)建所有的framgent
    @Override
    public Fragment getItem(int position) {
        int type = mList[position].getValue();
        switch (type) {
            case CHANNEL.MINE_ID:
                return MineFragment.newInstance();
            case CHANNEL.DISCOVERY_ID:
                return DiscoryFragment.newInstance();
            case CHANNEL.FRIEND_ID:
                return FriendFragment.newInstance();
//            case CHANNEL.VIDEO_ID:
//                return VideoFragment.newInstance();
        }
        return null;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.length;
    }
}

7克握、ViewPage初始化

        mViewPager = findViewById(R.id.view_pager);
        mAdapter = new HomePagerAdapter(getSupportFragmentManager(),CHANNELS);
        mViewPager.setAdapter(mAdapter);
        initMagicIndicator();
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蕾管,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子菩暗,更是在濱河造成了極大的恐慌掰曾,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,681評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件停团,死亡現(xiàn)場(chǎng)離奇詭異旷坦,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)佑稠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門秒梅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人舌胶,你說我怎么就攤上這事捆蜀。” “怎么了?”我有些...
    開封第一講書人閱讀 169,421評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵辆它,是天一觀的道長(zhǎng)誊薄。 經(jīng)常有香客問我,道長(zhǎng)锰茉,這世上最難降的妖魔是什么呢蔫? 我笑而不...
    開封第一講書人閱讀 60,114評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮飒筑,結(jié)果婚禮上片吊,老公的妹妹穿的比我還像新娘。我一直安慰自己扬霜,他們只是感情好定鸟,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,116評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著著瓶,像睡著了一般联予。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上材原,一...
    開封第一講書人閱讀 52,713評(píng)論 1 312
  • 那天沸久,我揣著相機(jī)與錄音,去河邊找鬼余蟹。 笑死卷胯,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的威酒。 我是一名探鬼主播窑睁,決...
    沈念sama閱讀 41,170評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼葵孤!你這毒婦竟也來了担钮?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,116評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤尤仍,失蹤者是張志新(化名)和其女友劉穎箫津,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宰啦,經(jīng)...
    沈念sama閱讀 46,651評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡苏遥,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,714評(píng)論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了赡模。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片田炭。...
    茶點(diǎn)故事閱讀 40,865評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖纺裁,靈堂內(nèi)的尸體忽然破棺而出诫肠,到底是詐尸還是另有隱情司澎,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布栋豫,位于F島的核電站挤安,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏丧鸯。R本人自食惡果不足惜蛤铜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,211評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望丛肢。 院中可真熱鬧围肥,春花似錦、人聲如沸蜂怎。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)杠步。三九已至氢伟,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間幽歼,已是汗流浹背朵锣。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留甸私,地道東北人诚些。 一個(gè)月前我還...
    沈念sama閱讀 49,299評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像皇型,于是被迫代替她去往敵國(guó)和親诬烹。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,870評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容