這篇文章主要介紹“Android中如何使用TabLayout+ViewPager實(shí)現(xiàn)底部導(dǎo)航欄”,在日常操作中,相信很多人在Android中如何使用TabLayout+ViewPager實(shí)現(xiàn)底部導(dǎo)航欄問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Android中如何使用TabLayout+ViewPager實(shí)現(xiàn)底部導(dǎo)航欄”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
我們提供的服務(wù)有:成都網(wǎng)站制作、網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、鎮(zhèn)賚ssl等。為近千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的鎮(zhèn)賚網(wǎng)站制作公司
布局
代碼
mViewPager = (ViewPager) view.findViewById(R.id.view_pager); mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout); initTabList(); mAdapter = new TabLayoutFragmentAdapter(getChildFragmentManager(), mTabList, getActivity(), mFragments, mTabImgs); mViewPager.setAdapter(mAdapter); mViewPager.setCurrentItem(0); mTabLayout.setupWithViewPager(mViewPager); mTabLayout.setTabMode(TabLayout.MODE_FIXED);//設(shè)置TabLayout的模式 for (int i = 0; i < mTabLayout.getTabCount(); i++) { mTabLayout.getTabAt(i).setCustomView(mAdapter.getTabView(i)); } mTabLayout.addOnTabSelectedListener(this);//設(shè)置TabLayout的選中監(jiān)聽
這里需要注意的就是TabLayout的使用。TabLayou配合ViewPager使用。要用 mTabLayout.setupWithViewPager(mViewPager);使二者聯(lián)系起來。另外這里面使用的是customView,當(dāng)然TabLayout自帶方法也可實(shí)現(xiàn)icon+text的效果。也就是效果:TabLayout + ViewPager 2
根據(jù)Tab選中狀態(tài)顯示Tab鍵效果
@Override public void onTabSelected(TabLayout.Tab tab) { setTabSelectedState(tab); } @Override public void onTabUnselected(TabLayout.Tab tab) { setTabUnSelectedState(tab); } @Override public void onTabReselected(TabLayout.Tab tab) { } private void setTabSelectedState(TabLayout.Tab tab) { View customView = tab.getCustomView(); TextView tabText = (TextView) customView.findViewById(R.id.tv_tab_text); ImageView tabIcon = (ImageView) customView.findViewById(R.id.iv_tab_icon); tabText.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); String s = tabText.getText().toString(); if (getString(R.string.item_home).equals(s)) { tabIcon.setImageResource(R.drawable.home_fill); } else if (getString(R.string.item_location).equals(s)) { tabIcon.setImageResource(R.drawable.location_fill); } else if (getString(R.string.item_like).equals(s)) { tabIcon.setImageResource(R.drawable.like_fill); } else if (getString(R.string.item_person).equals(s)) { tabIcon.setImageResource(R.drawable.person_fill); } } private void setTabUnSelectedState(TabLayout.Tab tab) { View customView = tab.getCustomView(); TextView tabText = (TextView) customView.findViewById(R.id.tv_tab_text); ImageView tabIcon = (ImageView) customView.findViewById(R.id.iv_tab_icon); tabText.setTextColor(ContextCompat.getColor(getActivity(), R.color.black_1)); String s = tabText.getText().toString(); if (getString(R.string.item_home).equals(s)) { tabIcon.setImageResource(R.drawable.home); } else if (getString(R.string.item_location).equals(s)) { tabIcon.setImageResource(R.drawable.location); } else if (getString(R.string.item_like).equals(s)) { tabIcon.setImageResource(R.drawable.like); } else if (getString(R.string.item_person).equals(s)) { tabIcon.setImageResource(R.drawable.person); } }
這里面不用設(shè)置defaultFragment,TabLayout會(huì)默認(rèn)顯示***個(gè);
另外,這里也貼出使用TabLayout自帶方法來實(shí)現(xiàn)效果代碼
值得說的是,自帶方法不能自定義icon和text的間距。用起來很方便,但是可能不是你要求的那個(gè)尺寸大小。我沒有去深究這里面的源碼。如果有人知道這個(gè)自帶方法怎么改變的,也請(qǐng)告知一下。
mViewPager = (ViewPager) view.findViewById(R.id.view_pager); mTabLayout = (TabLayout) view.findViewById(R.id.tab_layout); initTabList(); mAdapter = new TabLayoutFragment2Adapter(getChildFragmentManager(), mTabList, getActivity(), mFragments, mTabImgs); mViewPager.setAdapter(mAdapter); mViewPager.setCurrentItem(0); mTabLayout.setupWithViewPager(mViewPager); mTabLayout.setTabMode(TabLayout.MODE_FIXED); // for (int i = 0; i < mTabLayout.getTabCount(); i++) { // mTabLayout.getTabAt(i).setCustomView(mAdapter.getTabView(i)); // } mTabLayout.addOnTabSelectedListener(this); // mViewPager.setCurrentItem(0); mTabLayout.getTabAt(0).setIcon(R.drawable.home_fill);//自有方法添加icon mTabLayout.getTabAt(1).setIcon(R.drawable.location); mTabLayout.getTabAt(2).setIcon(R.drawable.like); mTabLayout.getTabAt(3).setIcon(R.drawable.person);
Tab切換
@Override public void onTabSelected(TabLayout.Tab tab) { // setTabSelectedState(tab);//這個(gè)也無需使用 resetTabIcon(); int position = tab.getPosition(); Log.e("Kevin", "position--->" + position); switch (position) { case 0: tab.setIcon(R.drawable.home_fill); break; case 1: tab.setIcon(R.drawable.location_fill); break; case 2: tab.setIcon(R.drawable.like_fill); break; case 3: tab.setIcon(R.drawable.person_fill); break; } } private void resetTabIcon() { mTabLayout.getTabAt(0).setIcon(R.drawable.home); mTabLayout.getTabAt(1).setIcon(R.drawable.location); mTabLayout.getTabAt(2).setIcon(R.drawable.like); mTabLayout.getTabAt(3).setIcon(R.drawable.person); }
到此,關(guān)于“Android中如何使用TabLayout+ViewPager實(shí)現(xiàn)底部導(dǎo)航欄”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!