關於activitygroup過時,用frament替換操作
現在Fragment的應用真的是越來越廣泛了,之前Android在3.0版本加入Fragment的時候,主要是為了解決Android Pad屏幕比較大,空間不能充分利用的問題,但現在即使隻是在手機上,也有很多的場景可以運用到Fragment了,今天我們就來學習其中一個特別棒的應用技巧。
很多手機應用都會有一個非常類似的功能,即屏幕的下方顯示一行Tab標簽選項,點擊不同的標簽就可以切換到不同的界麵,如以下幾個應用所示:
上麵三個應用從左到右分別是QQ、新浪微博和支付寶錢包,可見,這種底部標簽式的布局策略真的非常常見。
那麼話說回來,這種效果到底是如何的呢?熟悉Android的朋友一定都會知道,很簡單嘛,使用TabHost就OK了!但是殊不知,TabHost並非是那麼的簡單,它的可擴展性非常的差,不能隨意地定製Tab項顯示的內容,而且運行還要依賴於ActivityGroup。ActivityGroup原本主要是用於為每一個TabHost的子項管理一個單獨的Activity,但目前已經被廢棄了。為什麼呢?當然就是因為Fragment的出現了!查看Android官方文檔中ActivityGroup的描述,如下所示:
可以看到,在API 13的時候Android就已經將ActivityGroup廢棄掉了,並且官方推薦的替代方式就是使用Fragment,因為它使用起來更加的靈活。那麼剩下的問題就是如何借助Fragment來完成類似於TabHost一般的效果了,因此我們自然要動起手來了。
在開始之前,首先你必須已經了解Fragment的用法了,如果你對Fragment還比較陌生的話,建議先去閱讀我前麵的一篇文章 Android Fragment完全解析,關於碎片你所需知道的一切 。
另外,我們還應該準備好程序所需要的資源,比如說每一個Tab項中所用到的圖片。我已經事先從QQ裏截好了幾張圖作為這個項目的資源,稍後會連同源碼一起給出。
新建一個項目,起名就叫FragmentDemo,這裏我使用的是4.0的API。
下麵開始編程工作,這裏我們首先需要去編寫一個類似於QQ的主界麵,當然隻會去編寫界麵最下方的TabHost部分,而不會編寫上麵的內容界麵部分,因為內容界麵是應該寫在Fragment的布局裏的。打開或新建activity_main.xml作為程序的主布局文件,在裏麵加入如下代碼:
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <FrameLayout
- android:id="@+id/content"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1" >
- </FrameLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:background="@drawable/tab_bg" >
- <RelativeLayout
- android:id="@+id/message_layout"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/message_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:src="@drawable/message_unselected" />
- <TextView
- android:id="@+id/message_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:text="消息"
- android:textColor="#82858b" />
- </LinearLayout>
- </RelativeLayout>
- <RelativeLayout
- android:id="@+id/contacts_layout"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/contacts_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:src="@drawable/contacts_unselected" />
- <TextView
- android:id="@+id/contacts_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:text="聯係人"
- android:textColor="#82858b" />
- </LinearLayout>
- </RelativeLayout>
- <RelativeLayout
- android:id="@+id/news_layout"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/news_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:src="@drawable/news_unselected" />
- <TextView
- android:id="@+id/news_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:text="動態"
- android:textColor="#82858b" />
- </LinearLayout>
- </RelativeLayout>
- <RelativeLayout
- android:id="@+id/setting_layout"
- android:layout_width="0dp"
- android:layout_height="match_parent"
- android:layout_weight="1" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:orientation="vertical" >
- <ImageView
- android:id="@+id/setting_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:src="@drawable/setting_unselected" />
- <TextView
- android:id="@+id/setting_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:text="設置"
- android:textColor="#82858b" />
- </LinearLayout>
- </RelativeLayout>
- </LinearLayout>
- </LinearLayout>
既然是等分成了四分,那接下來我們自然要去分別實現四個Fragment和它們的布局了。新建一個message_layout.xml作為消息界麵的布局,代碼如下所示:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:src="@drawable/message_selected" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:padding="10dp"
- android:text="這是消息界麵"
- android:textSize="20sp" />
- </LinearLayout>
- </RelativeLayout>
然後要去創建對應這個布局的Fragment。新建MessageFragment繼承自Fragment,代碼如下所示:
- public class MessageFragment extends Fragment {
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View messageLayout = inflater.inflate(R.layout.message_layout, container, false);
- return messageLayout;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:src="@drawable/contacts_selected" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:padding="10dp"
- android:text="這是聯係人界麵"
- android:textSize="20sp" />
- </LinearLayout>
- </RelativeLayout>
- public class ContactsFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View contactsLayout = inflater.inflate(R.layout.contacts_layout,
- container, false);
- return contactsLayout;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:src="@drawable/news_selected" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:padding="10dp"
- android:text="這是動態界麵"
- android:textSize="20sp" />
- </LinearLayout>
- </RelativeLayout>
- public class NewsFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View newsLayout = inflater.inflate(R.layout.news_layout, container,
- false);
- return newsLayout;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:orientation="vertical" >
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:src="@drawable/setting_selected" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:padding="10dp"
- android:text="這是設置界麵"
- android:textSize="20sp" />
- </LinearLayout>
- </RelativeLayout>
- public class SettingFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View settingLayout = inflater.inflate(R.layout.setting_layout,
- container, false);
- return settingLayout;
- }
- }
- /**
- * 項目的主Activity,所有的Fragment都嵌入在這裏。
- *
- * @author guolin
- */
- public class MainActivity extends Activity implements OnClickListener {
- /**
- * 用於展示消息的Fragment
- */
- private MessageFragment messageFragment;
- /**
- * 用於展示聯係人的Fragment
- */
- private ContactsFragment contactsFragment;
- /**
- * 用於展示動態的Fragment
- */
- private NewsFragment newsFragment;
- /**
- * 用於展示設置的Fragment
- */
- private SettingFragment settingFragment;
- /**
- * 消息界麵布局
- */
- private View messageLayout;
- /**
- * 聯係人界麵布局
- */
- private View contactsLayout;
- /**
- * 動態界麵布局
- */
- private View newsLayout;
- /**
- * 設置界麵布局
- */
- private View settingLayout;
- /**
- * 在Tab布局上顯示消息圖標的控件
- */
- private ImageView messageImage;
- /**
- * 在Tab布局上顯示聯係人圖標的控件
- */
- private ImageView contactsImage;
- /**
- * 在Tab布局上顯示動態圖標的控件
- */
- private ImageView newsImage;
- /**
- * 在Tab布局上顯示設置圖標的控件
- */
- private ImageView settingImage;
- /**
- * 在Tab布局上顯示消息標題的控件
- */
- private TextView messageText;
- /**
- * 在Tab布局上顯示聯係人標題的控件
- */
- private TextView contactsText;
- /**
- * 在Tab布局上顯示動態標題的控件
- */
- private TextView newsText;
- /**
- * 在Tab布局上顯示設置標題的控件
- */
- private TextView settingText;
- /**
- * 用於對Fragment進行管理
- */
- private FragmentManager fragmentManager;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- // 初始化布局元素
- initViews();
- fragmentManager = getFragmentManager();
- // 第一次啟動時選中第0個tab
- setTabSelection(0);
- }
- /**
- * 在這裏獲取到每個需要用到的控件的實例,並給它們設置好必要的點擊事件。
- */
- private void initViews() {
- messageLayout = findViewById(R.id.message_layout);
- contactsLayout = findViewById(R.id.contacts_layout);
- newsLayout = findViewById(R.id.news_layout);
- settingLayout = findViewById(R.id.setting_layout);
- messageImage = (ImageView) findViewById(R.id.message_image);
- contactsImage = (ImageView) findViewById(R.id.contacts_image);
- newsImage = (ImageView) findViewById(R.id.news_image);
- settingImage = (ImageView) findViewById(R.id.setting_image);
- messageText = (TextView) findViewById(R.id.message_text);
- contactsText = (TextView) findViewById(R.id.contacts_text);
- newsText = (TextView) findViewById(R.id.news_text);
- settingText = (TextView) findViewById(R.id.setting_text);
- messageLayout.setOnClickListener(this);
- contactsLayout.setOnClickListener(this);
- newsLayout.setOnClickListener(this);
- settingLayout.setOnClickListener(this);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.message_layout:
- // 當點擊了消息tab時,選中第1個tab
- setTabSelection(0);
- break;
- case R.id.contacts_layout:
- // 當點擊了聯係人tab時,選中第2個tab
- setTabSelection(1);
- break;
- case R.id.news_layout:
- // 當點擊了動態tab時,選中第3個tab
- setTabSelection(2);
- break;
- case R.id.setting_layout:
- // 當點擊了設置tab時,選中第4個tab
- setTabSelection(3);
- break;
- default:
- break;
- }
- }
- /**
- * 根據傳入的index參數來設置選中的tab頁。
- *
- * @param index
- * 每個tab頁對應的下標。0表示消息,1表示聯係人,2表示動態,3表示設置。
- */
- private void setTabSelection(int index) {
- // 每次選中之前先清楚掉上次的選中狀態
- clearSelection();
- // 開啟一個Fragment事務
- FragmentTransaction transaction = fragmentManager.beginTransaction();
- // 先隱藏掉所有的Fragment,以防止有多個Fragment顯示在界麵上的情況
- hideFragments(transaction);
- switch (index) {
- case 0:
- // 當點擊了消息tab時,改變控件的圖片和文字顏色
- messageImage.setImageResource(R.drawable.message_selected);
- messageText.setTextColor(Color.WHITE);
- if (messageFragment == null) {
- // 如果MessageFragment為空,則創建一個並添加到界麵上
- messageFragment = new MessageFragment();
- transaction.add(R.id.content, messageFragment);
- } else {
- // 如果MessageFragment不為空,則直接將它顯示出來
- transaction.show(messageFragment);
- }
- break;
- case 1:
- // 當點擊了聯係人tab時,改變控件的圖片和文字顏色
- contactsImage.setImageResource(R.drawable.contacts_selected);
- contactsText.setTextColor(Color.WHITE);
- if (contactsFragment == null) {
- // 如果ContactsFragment為空,則創建一個並添加到界麵上
- contactsFragment = new ContactsFragment();
-
transaction.add(R.id.content, cont
最後更新:2017-04-03 05:39:40
上一篇:
Storm詳解一、Storm 概述
下一篇:
android 標簽雲的實現 關於x軸 冒泡排序~瞬間讓你高達上