實(shí)現(xiàn)android上方的導(dǎo)航條

整理自:
http://www.cnblogs.com/JohnTsai/p/4715454.html


直接切入正題,如果我們要實(shí)現(xiàn)下面這兩種效果該怎么做呢?


1

2

第一種效果還算比較簡(jiǎn)單拆讯,使用ViewPager再加上FragmentPagerAdapter就可以實(shí)現(xiàn)驯耻,其中,這其中最麻煩的就是tab下面那個(gè)導(dǎo)航條融击,需要根據(jù)屏幕大小以及標(biāo)簽個(gè)數(shù)來動(dòng)態(tài)設(shè)置其位置
而第二種效果除了導(dǎo)航條的位置的變換筑公,其長(zhǎng)度以及tab的位置都需要仔細(xì)地去考慮···真的是非常麻煩

之前一直想著自己能不能對(duì)這種效果進(jìn)行封裝,但是現(xiàn)在谷歌自己以及發(fā)布了一個(gè)庫-TabLayout尊浪,可以直接實(shí)現(xiàn)這樣的效果啦匣屡。這個(gè)TabLayout的具體知識(shí)就不在這說了,網(wǎng)上有很多資料拇涤。

最后附上如果是自己實(shí)現(xiàn)第一種那樣的效果怎么做:
因?yàn)樽约簩?shí)現(xiàn)不是我們這里的重點(diǎn)捣作,所以我就直接將代碼發(fā)上來就不解釋了

public class MySongFrag extends Fragment {
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.tv_album)
    TextView tvAlbum;
    @BindView(R.id.tv_singer)
    TextView tvSinger;
    @BindView(R.id.tv_song)
    TextView tvSong;
    @BindView(R.id.iv_tab_line)
    ImageView ivTabLine;
    @BindView(R.id.layout_tab)
    LinearLayout tabLayout;

    ResourceUtils resourceUtils;


    FragmentPagerAdapter fragmentPagerAdapter;
    List<Fragment> fragmentList=new ArrayList<Fragment>();

    //當(dāng)前顯示頁面的下標(biāo)
    double currentIndex;
    //屏幕寬度
    double screenWidth;
    //上面的那個(gè)tab的寬度,是屏幕寬度的六分之四
    double tabWidth;
    //屏幕寬度的六分之一
    double sixInOne;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.frag_my_song,null);
        ButterKnife.bind(this,view);

        init();
        initTabWidth();
        initViewPager();

        return view;
    }

    private void init() {
        resourceUtils=new ResourceUtils(getActivity());
    }

    //設(shè)置上面tab寬度為屏幕寬度三分之二
    private void initTabWidth() {
        screenWidth=getActivity().getWindowManager().getDefaultDisplay().getWidth();
        tabWidth=screenWidth*2/3;
        sixInOne=screenWidth/6;

        LinearLayout.LayoutParams lp= (LinearLayout.LayoutParams) tabLayout.getLayoutParams();
        lp.width= (int) tabWidth;
        tabLayout.setLayoutParams(lp);

        LinearLayout.LayoutParams lpIv= (LinearLayout.LayoutParams) ivTabLine.getLayoutParams();
        lpIv.width=(int) tabWidth/3;
        lpIv.leftMargin=(int) sixInOne;
        ivTabLine.setLayoutParams(lpIv);
    }

    private void initViewPager() {
        SongFrag songFrag=new SongFrag();
        SingerFrag singerFrag=new SingerFrag();
        AlbumFrag albumFrag=new AlbumFrag();

        fragmentList.add(songFrag);
        fragmentList.add(singerFrag);
        fragmentList.add(albumFrag);

        fragmentPagerAdapter=new FragmentPagerAdapter(getFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ivTabLine
                        .getLayoutParams();


                /**
        
                 * 利用currentIndex(當(dāng)前所在頁面)和position(下一個(gè)頁面)以及offset來
                 * 設(shè)置mTabLineIv的左邊距 滑動(dòng)場(chǎng)景:
                 * 記3個(gè)頁面,
                 * 從左到右分別為0,1,2
                 * 0->1; 1->2; 2->1; 1->0
                 */
                if (currentIndex == 0 && position == 0)// 0->1
                {
                    lp.leftMargin = (int) (sixInOne+positionOffset * (tabWidth * 1.0 / 3) + currentIndex
                            * (tabWidth / 3));
                } else if (currentIndex == 1 && position == 0) // 1->0
                {
                    lp.leftMargin = (int) (-(1 - positionOffset)
                            * (tabWidth * 1.0 / 3)+sixInOne + currentIndex
                            * (tabWidth / 3));

                } else if (currentIndex == 1 && position == 1) // 1->2
                {
                    lp.leftMargin = (int) (positionOffset * (tabWidth * 1.0 / 3) + sixInOne+currentIndex
                            * (tabWidth / 3));

                } else if (currentIndex == 2 && position == 1) // 2->1
                {
                    lp.leftMargin = (int) (-(1 - positionOffset)
                            * (tabWidth * 1.0 / 3) +sixInOne+ currentIndex
                            * (tabWidth / 3));
                }
                ivTabLine.setLayoutParams(lp);
            }

            @Override
            public void onPageSelected(int position) {
                resetTextView();
                switch (position) {
                    case 0:
                        tvSong.setTextColor(resourceUtils.getColorIntRes(R.color.myColorAccent));
                        break;
                    case 1:
                        tvSinger.setTextColor(resourceUtils.getColorIntRes(R.color.myColorAccent));
                        break;
                    case 2:
                        tvAlbum.setTextColor(resourceUtils.getColorIntRes(R.color.myColorAccent));
                        break;
                }
                currentIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void resetTextView() {
        tvAlbum.setTextColor(resourceUtils.getColorIntRes(R.color.tab_text_unchecked));
        tvSinger.setTextColor(resourceUtils.getColorIntRes(R.color.tab_text_unchecked));
        tvSong.setTextColor(resourceUtils.getColorIntRes(R.color.tab_text_unchecked));

    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鹅士,一起剝皮案震驚了整個(gè)濱河市券躁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖也拜,帶你破解...
    沈念sama閱讀 221,273評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件以舒,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡慢哈,警方通過查閱死者的電腦和手機(jī)蔓钟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來卵贱,“玉大人滥沫,你說我怎么就攤上這事〖悖” “怎么了兰绣?”我有些...
    開封第一講書人閱讀 167,709評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)编振。 經(jīng)常有香客問我缀辩,道長(zhǎng),這世上最難降的妖魔是什么踪央? 我笑而不...
    開封第一講書人閱讀 59,520評(píng)論 1 296
  • 正文 為了忘掉前任雌澄,我火速辦了婚禮,結(jié)果婚禮上杯瞻,老公的妹妹穿的比我還像新娘镐牺。我一直安慰自己,他們只是感情好魁莉,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,515評(píng)論 6 397
  • 文/花漫 我一把揭開白布睬涧。 她就那樣靜靜地躺著,像睡著了一般旗唁。 火紅的嫁衣襯著肌膚如雪畦浓。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,158評(píng)論 1 308
  • 那天检疫,我揣著相機(jī)與錄音讶请,去河邊找鬼。 笑死屎媳,一個(gè)胖子當(dāng)著我的面吹牛夺溢,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播烛谊,決...
    沈念sama閱讀 40,755評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼风响,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了丹禀?” 一聲冷哼從身側(cè)響起状勤,我...
    開封第一講書人閱讀 39,660評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤鞋怀,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后持搜,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體密似,經(jīng)...
    沈念sama閱讀 46,203評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,287評(píng)論 3 340
  • 正文 我和宋清朗相戀三年葫盼,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了辛友。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,427評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡剪返,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出邓梅,到底是詐尸還是另有隱情脱盲,我是刑警寧澤,帶...
    沈念sama閱讀 36,122評(píng)論 5 349
  • 正文 年R本政府宣布日缨,位于F島的核電站钱反,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏匣距。R本人自食惡果不足惜面哥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,801評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望毅待。 院中可真熱鬧尚卫,春花似錦、人聲如沸尸红。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽外里。三九已至怎爵,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間盅蝗,已是汗流浹背鳖链。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留墩莫,地道東北人芙委。 一個(gè)月前我還...
    沈念sama閱讀 48,808評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像狂秦,于是被迫代替她去往敵國(guó)和親题山。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,440評(píng)論 2 359

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