工廠模式加單例管理Fragment,同時解決fragment切換資料丟失問題
package com.itjfr.jfr.fragment;
import java.util.ArrayList;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
/**
* 本工廠管理fragment保證所有的fragment為單例
*
* @author Nullifier
*
*/
public class FragmentFactory {
private static Fragment settingFragment;
private static HomeFragment homeFragment;
private static GuidanceFragment guidanceFragment;
private static MarketFragment marketFragment;
private static MeFragment meFragment;
private static PersonDetailFragment personDetailFragment;
private static ScoreFragment scoreFragment;
private static ImageFragment imageFragment;
//當前fragment快取
private static Fragment cacheFragment;
public static Fragment getNowFragment() {
return cacheFragment;
}
/**
* 切換fragment工具方法 由於fragment經常切換如果使用replace方法則會造成fragment重新載入,所以只能hide/show
*
* @param target
* 目標切換的fragment
* @param resourceId
* 要替換的資源ID
* @param fagmentManager
* fragment管理者
*/
public static void switchPager(Fragment target, int resourceId,
FragmentManager fagmentManager) {
// 快取cache第一次進來是空的要單獨處理
if (cacheFragment == null) {
// 第一次進來要切換的fragment作為當前fragment加入到transaction中並顯示
fagmentManager.beginTransaction().add(resourceId, target)
.show(target).commit();
// 將此fragment賦值給快取fragment,停止程式碼執行
cacheFragment = target;
return;
}
// 如果目標fragment與當前fragment一樣則直接返回不執行
if (cacheFragment == target) {
return;
}
// 如果目標frragment沒有新增進來則新增進transaction中
if (!target.isAdded()) {
fagmentManager.beginTransaction().add(resourceId, target).commit();
}
// 隱藏當前的fragmeny顯示目標的fragment
fagmentManager.beginTransaction().hide(cacheFragment).show(target)
.commit();
//將目標fragment複製給快取標記
cacheFragment = target;
}
/**
* 獲取fragment
*
* @param fragment型別
* FragmentType列舉
* @return fragment
*/
public static Fragment getFragment(FragmentType type) {
switch (type) {
case GUIDANCE:
if (guidanceFragment == null) {
guidanceFragment = new GuidanceFragment();
}
return guidanceFragment;
case HOME:
if (homeFragment == null) {
homeFragment = new HomeFragment();
}
return homeFragment;
case MARKET:
if (marketFragment == null) {
marketFragment = new MarketFragment();
}
return marketFragment;
case ME:
if (meFragment == null) {
meFragment = new MeFragment();
}
return meFragment;
case PERSONDETAIL:
if (personDetailFragment == null) {
personDetailFragment = new PersonDetailFragment();
}
return personDetailFragment;
case SCORE:
if (scoreFragment == null) {
scoreFragment = new ScoreFragment();
}
return scoreFragment;
case SETTING:
if (settingFragment == null) {
settingFragment = new SettingFragment();
}
return settingFragment;
case IMAGE:
if (imageFragment == null) {
imageFragment = new ImageFragment();
}
return imageFragment;
default:
break;
}
return null;
}
/**
* 通過網路地址集合獲取fragment集合,集合不唯一
*
* @param type
* 型別 一般為IMAGE
* @param urlList
* 圖片集合
* @return
*/
public static List<Fragment> getFragment(FragmentType type,
List<String> urlList) {
switch (type) {
case IMAGE:
ArrayList<Fragment> listFragment = new ArrayList<Fragment>();
if (urlList != null && urlList.size() > 0) {
for (int i = 0; i < urlList.size(); i++) {
listFragment.add(new ImageFragment().setIamgeUrl(urlList
.get(i)));
}
}
return listFragment;
default:
break;
}
return null;
}
/**
* 列舉
*
* @author Nullifier
*
*/
public static enum FragmentType {
/**
* 導航
*/
GUIDANCE,
/**
* 主頁
*/
HOME,
/**
* 市場
*/
MARKET,
/**
* 我
*/
ME,
/**
* 個人詳情
*/
PERSONDETAIL,
/**
* 積分
*/
SCORE,
/**
* 設定
*/
SETTING,
/**
* 影象
*/
IMAGE;
}
}