本文(爭取做到)Android 最全的底部導航欄實現方法.
現在寫了4個主要方法.
還有一些個人感覺不完全切題的方法也會簡單介紹一下.
方法一. ViewPager + List<View> + PagerAdapter
先看activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="45dp"
- android:background="#0E6DB0"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="@string/app_name"
- android:textColor="#ffffff"
- android:textSize="20sp"
- android:textStyle="bold" />
- </LinearLayout>
- <android.support.v4.view.ViewPager
- android:id="@+id/viewPager"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1" >
- </android.support.v4.view.ViewPager>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="55dp"
- android:background="#0E6DB0"
- android:orientation="horizontal" >
- <LinearLayout
- android:id="@+id/llChat"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/ivChat"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00000000"
- android:src="@drawable/tab_chat" />
- <TextView
- android:id="@+id/tvChat"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="微信"
- android:textColor="@drawable/tab_textview" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/llFriends"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/ivFriends"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00000000"
- android:src="@drawable/tab_friends" />
- <TextView
- android:id="@+id/tvFriends"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="朋友"
- android:textColor="@drawable/tab_textview" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/llContacts"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/ivContacts"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00000000"
- android:src="@drawable/tab_contacts" />
- <TextView
- android:id="@+id/tvContacts"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="通訊錄"
- android:textColor="@drawable/tab_textview" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/llSettings"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/ivSettings"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00000000"
- android:src="@drawable/tab_setting" />
- <TextView
- android:id="@+id/tvSettings"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="設置"
- android:textColor="@drawable/tab_textview" />
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>

說明一:還有另一種方式是用RadioGroup的方式,但是那種方式如果以后要包含小紅點提醒或者右上角數字角標提醒,就不好靈活的修改了.所以本文忽略那種方法.
說明二:底部導航欄的4個ImageView使用的src, TextView使用的textColor都是seletor
然后看MainActivity.java
- package com.yao.tab01;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.support.v4.view.ViewPager;
- import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class MainActivity extends Activity implements OnClickListener {
- private List<View> views = new ArrayList<View>();
- private ViewPager viewPager;
- private LinearLayout llChat, llFriends, llContacts, llSettings;
- private ImageView ivChat, ivFriends, ivContacts, ivSettings, ivCurrent;
- private TextView tvChat, tvFriends, tvContacts, tvSettings, tvCurrent;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- initView();
- initData();
- }
- private void initView() {
- viewPager = (ViewPager) findViewById(R.id.viewPager);
- llChat = (LinearLayout) findViewById(R.id.llChat);
- llFriends = (LinearLayout) findViewById(R.id.llFriends);
- llContacts = (LinearLayout) findViewById(R.id.llContacts);
- llSettings = (LinearLayout) findViewById(R.id.llSettings);
- llChat.setOnClickListener(this);
- llFriends.setOnClickListener(this);
- llContacts.setOnClickListener(this);
- llSettings.setOnClickListener(this);
- ivChat = (ImageView) findViewById(R.id.ivChat);
- ivFriends = (ImageView) findViewById(R.id.ivFriends);
- ivContacts = (ImageView) findViewById(R.id.ivContacts);
- ivSettings = (ImageView) findViewById(R.id.ivSettings);
- tvChat = (TextView) findViewById(R.id.tvChat);
- tvFriends = (TextView) findViewById(R.id.tvFriends);
- tvContacts = (TextView) findViewById(R.id.tvContacts);
- tvSettings = (TextView) findViewById(R.id.tvSettings);
- ivChat.setSelected(true);
- tvChat.setSelected(true);
- ivCurrent = ivChat;
- tvCurrent = tvChat;
- viewPager.setOnPageChangeListener(new OnPageChangeListener() {
- @Override
- public void onPageSelected(int position) {
- changeTab(position);
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- }
- });
- }
- private void initData() {
- LayoutInflater mInflater = LayoutInflater.from(this);
- View tab01 = mInflater.inflate(R.layout.tab01, null);
- View tab02 = mInflater.inflate(R.layout.tab02, null);
- View tab03 = mInflater.inflate(R.layout.tab03, null);
- View tab04 = mInflater.inflate(R.layout.tab04, null);
- views.add(tab01);
- views.add(tab02);
- views.add(tab03);
- views.add(tab04);
- MyPagerAdapter adapter = new MyPagerAdapter(views);
- viewPager.setAdapter(adapter);
- }
- @Override
- public void onClick(View v) {
- changeTab(v.getId());
- }
- private void changeTab(int id) {
- ivCurrent.setSelected(false);
- tvCurrent.setSelected(false);
- switch (id) {
- case R.id.llChat:
- viewPager.setCurrentItem(0);
- case 0:
- ivChat.setSelected(true);
- ivCurrent = ivChat;
- tvChat.setSelected(true);
- tvCurrent = tvChat;
- break;
- case R.id.llFriends:
- viewPager.setCurrentItem(1);
- case 1:
- ivFriends.setSelected(true);
- ivCurrent = ivFriends;
- tvFriends.setSelected(true);
- tvCurrent = tvFriends;
- break;
- case R.id.llContacts:
- viewPager.setCurrentItem(2);
- case 2:
- ivContacts.setSelected(true);
- ivCurrent = ivContacts;
- tvContacts.setSelected(true);
- tvCurrent = tvContacts;
- break;
- case R.id.llSettings:
- viewPager.setCurrentItem(3);
- case 3:
- ivSettings.setSelected(true);
- ivCurrent = ivSettings;
- tvSettings.setSelected(true);
- tvCurrent = tvSettings;
- break;
- default:
- break;
- }
- }
- }
這種方法一的方式就是提前用mInflater.inflate4個View丟到PagerAdapter里面,再給ViewPager設置Adapter
然后把點擊底部Tab的事件和滑動ViewPager的事件(主要包括圖片和文字seletor切換)整合在一起.
方法二. ViewPager + List<Fragment> + FragmentPagerAdapter或FragmentStatePagerAdapter
這種方法就很常見了
activity_main.xml和上文一樣.
我們看MainActivity.java
- package com.yao.tab02;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.support.v4.view.ViewPager;
- import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.view.View;
- import android.view.Window;
- import android.view.View.OnClickListener;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class MainActivity extends Activity implements OnClickListener {
- private List<Fragment> fragments = new ArrayList<Fragment>();
- private ViewPager viewPager;
- private LinearLayout llChat, llFriends, llContacts, llSettings;
- private ImageView ivChat, ivFriends, ivContacts, ivSettings, ivCurrent;
- private TextView tvChat, tvFriends, tvContacts, tvSettings, tvCurrent;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- initView();
- initData();
- }
- private void initView() {
- viewPager = (ViewPager) findViewById(R.id.viewPager);
- llChat = (LinearLayout) findViewById(R.id.llChat);
- llFriends = (LinearLayout) findViewById(R.id.llFriends);
- llContacts = (LinearLayout) findViewById(R.id.llContacts);
- llSettings = (LinearLayout) findViewById(R.id.llSettings);
- llChat.setOnClickListener(this);
- llFriends.setOnClickListener(this);
- llContacts.setOnClickListener(this);
- llSettings.setOnClickListener(this);
- ivChat = (ImageView) findViewById(R.id.ivChat);
- ivFriends = (ImageView) findViewById(R.id.ivFriends);
- ivContacts = (ImageView) findViewById(R.id.ivContacts);
- ivSettings = (ImageView) findViewById(R.id.ivSettings);
- tvChat = (TextView) findViewById(R.id.tvChat);
- tvFriends = (TextView) findViewById(R.id.tvFriends);
- tvContacts = (TextView) findViewById(R.id.tvContacts);
- tvSettings = (TextView) findViewById(R.id.tvSettings);
- ivChat.setSelected(true);
- tvChat.setSelected(true);
- ivCurrent = ivChat;
- tvCurrent = tvChat;
- viewPager.setOnPageChangeListener(new OnPageChangeListener() {
- @Override
- public void onPageSelected(int position) {
- changeTab(position);
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- }
- });
- viewPager.setOffscreenPageLimit(2); //設置向左和向右都緩存limit個頁面
- }
- private void initData() {
- Fragment chatFragment = new ChatFragment();
- Fragment friendsFragment = new FriendsFragment();
- Fragment contactsFragment = new ContactsFragment();
- Fragment settingsFragment = new SettingsFragment();
- fragments.add(chatFragment);
- fragments.add(friendsFragment);
- fragments.add(contactsFragment);
- fragments.add(settingsFragment);
- MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getFragmentManager(), fragments);
- // MyFragmentStatePagerAdapter adapter = new MyFragmentStatePagerAdapter(getFragmentManager(), fragments);
- viewPager.setAdapter(adapter);
- }
- @Override
- public void onClick(View v) {
- changeTab(v.getId());
- }
- private void changeTab(int id) {
- ivCurrent.setSelected(false);
- tvCurrent.setSelected(false);
- switch (id) {
- case R.id.llChat:
- viewPager.setCurrentItem(0);
- case 0:
- ivChat.setSelected(true);
- ivCurrent = ivChat;
- tvChat.setSelected(true);
- tvCurrent = tvChat;
- break;
- case R.id.llFriends:
- viewPager.setCurrentItem(1);
- case 1:
- ivFriends.setSelected(true);
- ivCurrent = ivFriends;
- tvFriends.setSelected(true);
- tvCurrent = tvFriends;
- break;
- case R.id.llContacts:
- viewPager.setCurrentItem(2);
- case 2:
- ivContacts.setSelected(true);
- ivCurrent = ivContacts;
- tvContacts.setSelected(true);
- tvCurrent = tvContacts;
- break;
- case R.id.llSettings:
- viewPager.setCurrentItem(3);
- case 3:
- ivSettings.setSelected(true);
- ivCurrent = ivSettings;
- tvSettings.setSelected(true);
- tvCurrent = tvSettings;
- break;
- default:
- break;
- }
- }
- }
1.FragmentStatePagerAdapter : 適合多個界面,類似於listView原理,離開視線就會被回收 會執行onDestroyView方法 onDestroy方法
2.FragmentPagerAdapter : 適合少量界面, 方便滑動 執行onDestroyView方法 不執行onDestroy方法
3.兩者都會預先准備好並緩存上一個和下一個Fragment
說明二:重要說明:有個神方法viewPager.setOffscreenPageLimit(2); //設置向左和向右都緩存limit個頁面.
我也是很晚才發現有這個方法.下面4個Tab, 只要你設置這個值為3, 那4個Tab永遠都會緩存着了.
變態說明:這里告訴大家一個小技巧.ViewPager是V4包里面的.用到的FragmentPagerAdapter和FragmentStatePagerAdapter也是V4包里面的.
如果我們不想用android.support.v4.app.Fragment, 那就可以自己復制出來一個FragmentPagerAdapter,然后把里面的Fragment改成android.app.Fragment.
連帶FragmentManager和FragmentTransaction也要改成android.app包下的
跳過方法三,先講方法四.方法四是代碼量最少的方法.簡單快捷輕便
item_tab.xml 這個是下面部分4個Tab中其中一個Tab的布局, 以此來FragmentTabHost會幫忙批量生產出4個Tab
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:orientation="vertical"
- android:padding="5dp" >
- <ImageView
- android:id="@+id/iv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00000000"
- android:src="@drawable/tab_setting" />
- <TextView
- android:id="@+id/tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="@drawable/tab_textview" />
- </LinearLayout>
activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="45dp"
- android:background="#0E6DB0"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="@string/app_name"
- android:textColor="#ffffff"
- android:textSize="20sp"
- android:textStyle="bold" />
- </LinearLayout>
- <FrameLayout
- android:id="@+id/flContainer"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1" >
- </FrameLayout>
- <com.yao.tab04.FragmentTabHost
- android:id="@+id/tabhost"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="#0E6DB0" >
- </com.yao.tab04.FragmentTabHost>
- </LinearLayout>
MainActivity.java
- package com.yao.tab04;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.Window;
- import android.widget.ImageView;
- import android.widget.TabHost.OnTabChangeListener;
- import android.widget.TabHost.TabSpec;
- import android.widget.TextView;
- public class MainActivity extends Activity implements OnTabChangeListener {
- private FragmentTabHost tabHost;
- private String[] tabText = { "聊天", "朋友", "通訊錄", "設置" };
- private int[] imageRes = new int[] { R.drawable.tab_chat, R.drawable.tab_friends, R.drawable.tab_contacts, R.drawable.tab_setting };
- private Class[] fragments = new Class[] { ChatFragment.class, FriendsFragment.class, ContactsFragment.class, SettingsFragment.class };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- tabHost = (FragmentTabHost) super.findViewById(R.id.tabhost);
- tabHost.setup(this, super.getFragmentManager(), R.id.flContainer);
- tabHost.getTabWidget().setDividerDrawable(null);
- tabHost.setOnTabChangedListener(this);
- initTab();
- }
- private void initTab() {
- for (int i = 0; i < tabText.length; i++) {
- View view = LayoutInflater.from(this).inflate(R.layout.item_tab, null);
- ((TextView) view.findViewById(R.id.tv)).setText(tabText[i]);
- ((ImageView) view.findViewById(R.id.iv)).setImageResource(imageRes[i]);
- TabSpec tabSpec = tabHost.newTabSpec(tabText[i]).setIndicator(view);
- tabHost.addTab(tabSpec, fragments[i], null);
- tabHost.setTag(i);
- }
- }
- //自動把getCurrentTabView下的所有子View的selected狀態設為true. 牛逼!
- @Override
- public void onTabChanged(String tabId) {
- //首次打開自動會調用一下 首次自動輸出tabId : 聊天
- Log.e("yao", "tabId : " + tabId);
- // TabWidget tabWidget = tabHost.getTabWidget(); //獲取整個底部Tab的布局, 可以通過tabWidget.getChildCount和tabWidget.getChildAt來獲取某個子View
- // int pos = tabHost.getCurrentTab(); //獲取當前tab的位置
- // View view = tabHost.getCurrentTabView(); //獲取當前tab的view
- }
- }
說明一: 重要說明:有些博主還需要寫一個未選中的圖片ResId數組和一個選中的圖片ResId數組.然后根據點擊自己在代碼上寫把圖片切換成選中狀態.Navie.
你只要傳進去一組seletor圖片,把文字顏色也設為seletor.FragmentTabHost叼的地方來了.它會自動把圖片這個布局View下的所有子控件切換成selected狀態.
所以我代碼這些相關的邏輯沒寫,還是會變色.
說明二: 業務邏輯寫在onTabChanged. 一般用到的幾個核心方法 已經寫在上面了
說明三: TabHost好像不能設置導航欄在底部. 所以只能用FragmentTabHost.
說明四: 切到別的頁面,當前這個頁面會執行到onDestroyView方法,不會執行onDestroy方法.
方法三. 用fragmentTransaction的show和hide方法隱藏和顯示Fragment
網上這種代碼已有,但是大多數寫的很亂.
大致思路整理一下就是如下.
- if (想要的fragment == null) {
- if (當前顯示的fragment != null) {
- 隱藏當前fragment
- }
- 生產想要的fragment
- } else if (想要的fragment == 當前顯示的fragment) {
- } else {
- 隱藏當前fragment
- 顯示想要的fragment
- }
下面看代碼.
activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="45dp"
- android:background="#0E6DB0"
- android:gravity="center"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="@string/app_name"
- android:textColor="#ffffff"
- android:textSize="20sp"
- android:textStyle="bold" />
- </LinearLayout>
- <FrameLayout
- android:id="@+id/flContainer"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1" >
- </FrameLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="55dp"
- android:background="#0E6DB0"
- android:orientation="horizontal" >
- <LinearLayout
- android:id="@+id/llChat"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/ivChat"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00000000"
- android:src="@drawable/tab_chat" />
- <TextView
- android:id="@+id/tvChat"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="微信"
- android:textColor="@drawable/tab_textview" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/llFriends"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/ivFriends"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00000000"
- android:src="@drawable/tab_friends" />
- <TextView
- android:id="@+id/tvFriends"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="朋友"
- android:textColor="@drawable/tab_textview" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/llContacts"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/ivContacts"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00000000"
- android:src="@drawable/tab_contacts" />
- <TextView
- android:id="@+id/tvContacts"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="通訊錄"
- android:textColor="@drawable/tab_textview" />
- </LinearLayout>
- <LinearLayout
- android:id="@+id/llSettings"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:gravity="center"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/ivSettings"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00000000"
- android:src="@drawable/tab_setting" />
- <TextView
- android:id="@+id/tvSettings"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="設置"
- android:textColor="@drawable/tab_textview" />
- </LinearLayout>
- </LinearLayout>
- </LinearLayout>
本人核心封裝
FragmentSwitchTool.java
- /**
- * FileName:FragmentSwitchTool.java
- * Copyright YaoDiWei All Rights Reserved.
- */
- package com.yao.tab03;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Fragment;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- /**
- * @author YaoDiWei
- * @version
- */
- public class FragmentSwitchTool implements OnClickListener {
- private FragmentManager fragmentManager;
- private Fragment currentFragment;
- // private View currentClickableView;
- private View[] currentSelectedView;
- private View[] clickableViews; //傳入用於被點擊的view,比如是一個LinearLayout
- private List<View[]> selectedViews; //傳入用於被更改資源selected狀態的view[], 比如一組View[]{TextView, ImageView}
- private Class<? extends Fragment>[] fragments;
- private Bundle[] bundles;
- private int containerId;
- private boolean showAnimator;
- public FragmentSwitchTool(FragmentManager fragmentManager, int containerId) {
- super();
- this.fragmentManager = fragmentManager;
- this.containerId = containerId;
- }
- public void setClickableViews(View... clickableViews) {
- this.clickableViews = clickableViews;
- for (View view : clickableViews) {
- view.setOnClickListener(this);
- }
- }
- public void setSelectedViews(List<View[]> selectedViews) {
- this.selectedViews = selectedViews;
- }
- public FragmentSwitchTool addSelectedViews(View... views){
- if (selectedViews == null) {
- selectedViews = new ArrayList<View[]>();
- }
- selectedViews.add(views);
- return this;
- }
- public void setFragments(Class<? extends Fragment>... fragments) {
- this.fragments = fragments;
- }
- public void setBundles(Bundle... bundles) {
- this.bundles = bundles;
- }
- public void changeTag(View v) {
- FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
- Fragment fragment = fragmentManager.findFragmentByTag(String.valueOf(v.getId()));
- for (int i = 0; i < clickableViews.length; i++) {
- if (v.getId() == clickableViews[i].getId()) {
- //過渡動畫
- if (showAnimator) {
- int exitIndex = selectedViews.indexOf(currentSelectedView);
- // Log.e("yao", "enter : " + i + " exit: " + exitIndex);
- if (i > exitIndex){
- fragmentTransaction.setCustomAnimations(R.anim.slide_right_in, R.anim.slide_left_out);
- } else if (i < exitIndex) {
- fragmentTransaction.setCustomAnimations(R.anim.slide_left_in, R.anim.slide_right_out);
- }
- }
- //過渡動畫
- if (fragment == null) {
- if (currentFragment != null) {
- fragmentTransaction.hide(currentFragment);
- for (View view : currentSelectedView) {
- view.setSelected(false);
- }
- }
- try {
- fragment = fragments[i].newInstance();
- if (bundles != null && bundles[i] != null) {
- fragment.setArguments(bundles[i]);
- }
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- fragmentTransaction.add(containerId, fragment, String.valueOf(clickableViews[i].getId()));
- } else if (fragment == currentFragment) {
- } else {
- fragmentTransaction.hide(currentFragment);
- for (View view : currentSelectedView) {
- view.setSelected(false);
- }
- fragmentTransaction.show(fragment);
- }
- fragmentTransaction.commit();
- currentFragment = fragment;
- for (View view : selectedViews.get(i)) {
- view.setSelected(true);
- }
- currentSelectedView = selectedViews.get(i);
- break;
- }
- }
- }
- @Override
- public void onClick(View v)
- {
- changeTag(v);
- }
- }
MainActivity.java
- package com.yao.tab03;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class MainActivity extends Activity{
- private LinearLayout llChat, llFriends, llContacts, llSettings;
- private ImageView ivChat, ivFriends, ivContacts, ivSettings;
- private TextView tvChat, tvFriends, tvContacts, tvSettings;
- private FragmentSwitchTool tool;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- initView();
- tool = new FragmentSwitchTool(getFragmentManager(), R.id.flContainer);
- tool.setClickableViews(llChat, llFriends, llContacts, llSettings);
- tool.addSelectedViews(new View[]{ivChat, tvChat}).addSelectedViews(new View[]{ivFriends, tvFriends})
- .addSelectedViews(new View[]{ivContacts, tvContacts}).addSelectedViews(new View[]{ivSettings, tvSettings});
- tool.setFragments(ChatFragment.class, FriendsFragment.class, ContactsFragment.class, SettingsFragment.class);
- tool.changeTag(llChat);
- }
- private void initView() {
- llChat = (LinearLayout) findViewById(R.id.llChat);
- llFriends = (LinearLayout) findViewById(R.id.llFriends);
- llContacts = (LinearLayout) findViewById(R.id.llContacts);
- llSettings = (LinearLayout) findViewById(R.id.llSettings);
- ivChat = (ImageView) findViewById(R.id.ivChat);
- ivFriends = (ImageView) findViewById(R.id.ivFriends);
- ivContacts = (ImageView) findViewById(R.id.ivContacts);
- ivSettings = (ImageView) findViewById(R.id.ivSettings);
- tvChat = (TextView) findViewById(R.id.tvChat);
- tvFriends = (TextView) findViewById(R.id.tvFriends);
- tvContacts = (TextView) findViewById(R.id.tvContacts);
- tvSettings = (TextView) findViewById(R.id.tvSettings);
- }
- }
說明一: 重要說明:FragmentSwitchTool把所有的操作都封裝好在里面了.所以以后只需要寫一個布局文件(注意要寫成seletor的形式).
在MainActivity中寫幾行給FragmentSwitchTool的傳參就行.
說明二: 過渡動畫有點糙,隨便寫的,可以刪掉.
Bottom Navigation是5.0(API level 21)新出的一種符合MD規范的導航欄規范。
注意這里說的是規范,所以當時並沒有實現這個BottomNavigation這里有兩篇文章可以看看。
原文: https://material.google.com/components/bottom-navigation.html
譯文:https://modao.cc/posts/3068

然后在Android Support Library 25 后,Android 終於自己增加了一個控件 android.support.design.widget.BottomNavigationView。用法很簡單,你甚至不用去看文檔。直接在Android studio 里新建一個 BottomNavigation 的模板 Activity 就行。
然而官方這個相比網上那三個開源庫,動畫就相對朴素了。

總結:
方法一:創建就會一次性加載完四個頁面.適合簡單的頁面,比如app一開始的導航頁.
方法二:給力的方法.適合能左右滑動的頁面.可以自己定制緩存策略.配合神方法,也能一開始就加載全部頁面.
方法三:最大的好處是, 用的才加載. 一旦加載就不刪除. 切換只用hide和show,速度飛快. 當然你也可以自己定制適合自己的緩存策略.
方法四:簡單快捷.代碼少.但是切換速度理論不夠方法三快.
方法五:符合MD設計的,希望自己的APP炫酷一點的.毫無疑問都應該用BottomNavigation規范的控件。
下載地址:
http://download.csdn.net/detail/alcoholdi/9565976