Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 Fragment 做選項卡:一次完整的動態切換實踐

編輯:關於 Android 程式設計

本文把之前講過的「動態創建 Fragment」拿來實踐一次,做一個四頁新聞切換的選項卡。

項目布局

整體結構很簡單:上面一條橫向 LinearLayout 放四個 TextView 當標籤,下面一個空的 LinearLayout 作為 Fragment 的容器。

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 xmlns:tools="https://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity" >

 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >
  <TextView android:id="@+id/tab1" android:layout_width="0dip"
   android:layout_height="wrap_content" android:layout_weight="1"
   android:gravity="center" android:text="社會新聞" />
  <TextView android:id="@+id/tab2" android:layout_width="0dip"
   android:layout_height="wrap_content" android:layout_weight="1"
   android:gravity="center" android:text="生活新聞" />
  <TextView android:id="@+id/tab3" android:layout_width="0dip"
   android:layout_height="wrap_content" android:layout_weight="1"
   android:gravity="center" android:text="軍事新聞" />
  <TextView android:id="@+id/tab4" android:layout_width="0dip"
   android:layout_height="wrap_content" android:layout_weight="1"
   android:gravity="center" android:text="娛樂新聞" />
 </LinearLayout>

 <LinearLayout
  android:id="@+id/content"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >
 </LinearLayout>
</LinearLayout> 

四個 TextView 都用 layout_width="0dip" 配 layout_weight="1",這樣它們會等分整行寬度,比寫死 dp 值更能適配不同屏幕。

四個 Fragment

新建 Fragment1.java 到 Fragment4.java,每個只負責 inflate 自己的布局:

 public class Fragment1 extends Fragment {
 @Override public View onCreateView(LayoutInflater inflater,
   ViewGroup container, Bundle savedInstanceState) {
  return inflater.inflate(R.layout.fragment1, null);
 }
 }

其餘三個文件寫法相同。對應的 fragment1.xml 到 fragment4.xml 也很簡單,居中顯示一行文字即可:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:gravity="center"
 android:orientation="vertical" >
 <TextView android:id="@+id/textview1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="社會新聞"
  android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout> 

MainActivity 中的切換邏輯

 public class MainActivity extends Activity implements OnClickListener {
 private LinearLayout content;
 private TextView tv1, tv2, tv3, tv4;
 private FragmentManager fm;
 private FragmentTransaction ft;

 @Override protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  content = (LinearLayout) findViewById(R.id.content);
  tv1 = (TextView) findViewById(R.id.tab1);
  tv2 = (TextView) findViewById(R.id.tab2);
  tv3 = (TextView) findViewById(R.id.tab3);
  tv4 = (TextView) findViewById(R.id.tab4);
  tv1.setOnClickListener(this);
  tv2.setOnClickListener(this);
  tv3.setOnClickListener(this);
  tv4.setOnClickListener(this);
  fm = getFragmentManager();
  ft = fm.beginTransaction();
  ft.replace(R.id.content, new Fragment1()); // 默認顯示 Fragment1
  ft.commit();
 }

 @Override public void onClick(View v) {
  ft = fm.beginTransaction();
  switch (v.getId()) {
  case R.id.tab1: ft.replace(R.id.content, new Fragment1()); break;
  case R.id.tab2: ft.replace(R.id.content, new Fragment2()); break;
  case R.id.tab3: ft.replace(R.id.content, new Fragment3()); break;
  case R.id.tab4: ft.replace(R.id.content, new Fragment4()); break;
  default: break;
  }
  ft.commit();
 }
 } 

運行效果:

這個例子刻意保持精簡,只用了 replace()。如果希望切換時保留各頁狀態、返回鍵能逐級回退,可以改用 addToBackStack() 配合 hide/show;若需要切換動畫,則在 commit 前調用 setCustomAnimations()。另外原示例漏掉了 onCreate 中的 commit,這裡已補上,否則默認頁不會顯示。

附錄:原文附帶的一段前端模組封裝代碼

 /**
  * Created by gerry.zhong on 2016/10/11.
  */
var gerry = (function(){
 // 創建一個獨立的對象,注入所有的方法,包括你想拋出去和不想拋出去的
 var tool = {
  AAAA:function(){},
  BBBB:function(){
   console.log("我只想內部使用,不想給別人用");
  }
 };
 /*
  * 該對象承載所有需要拋出去的對象
  * 1. 該對象中的方法可以自己寫
  * 2. 該對象中的方法可以注入(例子中的 tempObj.tool.AA)
  * 3. 該對象也可以選擇性拋出給使用者需要的方法,也可以隱藏(tool.BBBB)
  */
 var tempObj = {
  // reader 為一些初始化需要的操作,有時候會有注冊事件等
  reader:function(){ },
  // 注入所有的選擇器,方便選擇器變化時直接修改該對象
  selector:{
   mySelector:"#mySelector",
  },
  // 注入所有的接口地址,方便接口變化時快速變更
  interface:{
   loginUrl:"",
  },
  // 注入 page 中所有的事件,統一管理
  registerEle:{
   click_login:function(){
    // 注冊單擊事件
   }
  },
  // 注入所有 ajax 請求,頁面所有請求將在這裡統一管理
  ajaxRequest:{
   // 不公用一個請求方案
   ajax_login:function(){
    $.post("","",function(data){
     tempObj.callback.call_login(data);
    });
   },
   // 會有多個業務公用這個請求
   ajax_login_T:function(callback){
    $.post("","",callback);
   },
  },
  // 處理所有回調函數
  callback:{
   call_login:function(data){
    // 處理回調
   },
   call_login_T:function(){
    var temp = function(){ };
    tempObj.ajaxRequest.ajax_login_T(temp);
   }
  },
  // 所有使用的工具類
  tool:{
   A:function(){
    console.log("我是自己寫的方法");
   },
   AA:tool.AAAA,   // 這是我想拋出去給別人用的東西
  },
  // 臨時緩存存放區域,僅針對本頁面
  temp:{ },
  // 業務使用區域,把上面所有原子按業務串起來
  firm:{ }
 };
 /*
  * 閉包拋出去的方法
  */
 var outputObj = function(){
  // 首先執行 reader 方法,初始化一些操作
  tempObj.reader();
  return tempObj;
 };
 return new outputObj();
})(); 
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved