Fragment管理工具類
阿新 • • 發佈:2019-01-23
分享一個之前專案自己寫的一個Fragment切換工具類
- 首先是工具類的程式碼,工具類封裝了Fragment的切換,新增、銷燬、狀態儲存等方法
public class FragmentTabManager {
private Context mContext;
private FragmentManager mFragmentManager;
private int mContainerId;
private ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private TabInfo mLastTab;//當前手機顯示的選項卡
private TabHost.OnTabChangeListener mOnTabChangeListener;
public FragmentTabManager(Context context, FragmentManager fragmentManager, int containerId) {
mContext = context;
mFragmentManager = fragmentManager;
mContainerId = containerId;
}
/**
* 新增選項卡
*
* @param tag fragment的tag
* @param clazz fragment的class
* @param args fragment引數
* @return
*/
public FragmentTabManager addTab(String tag, Class<?> clazz, Bundle args) {
TabInfo tabInfo = new TabInfo(tag, clazz, args);
mTabs.add(tabInfo);
return this;
}
/**
* 獲取當前的fragment
*
* @return
*/
public Fragment getCurrentFragment() {
return mLastTab == null ? null : mLastTab.fragment;
}
/**
* 獲取當前選項卡的tag
*
* @return
*/
public String getCurrentTag() {
return mLastTab == null ? null : mLastTab.tag;
}
/**
* 設定tab變化監聽
*
* @param listener
*/
public void setOnTabChangedListener(TabHost.OnTabChangeListener listener) {
mOnTabChangeListener = listener;
}
/**
* 解除當前fragment的viewTree
*/
public void detach() {
if (mLastTab != null) {
if (mLastTab.fragment != null) {
mFragmentManager.beginTransaction().detach(mLastTab.fragment).commit();
}
mLastTab = null;
}
}
/**
* 儲存fragment的狀態
*
* @param tabId
*/
public void restoreTab(String tabId) {
TabInfo newTab = null;
for (int i = 0; i < mTabs.size(); i++) {
TabInfo tab = mTabs.get(i);
tab.fragment = mFragmentManager.findFragmentByTag(tab.tag);
if (tabId.equals(tab.tag)) {
newTab = tab;
}
}
mLastTab = newTab;
}
/**
* 根據tabId切換fragment
*
* @param tabId 需要顯示的fragment的tabId
*/
public void changeTab(String tabId) {
FragmentTransaction ft = doTabChange(tabId, null);
if (ft != null)
ft.commit();
if (mOnTabChangeListener != null)
mOnTabChangeListener.onTabChanged(tabId);
}
private FragmentTransaction doTabChange(String tabId, FragmentTransaction ft) {
TabInfo newTab = null;
for (int i = 0; i < mTabs.size(); i++) {
TabInfo tabInfo = mTabs.get(i);
if (tabInfo.tag.equalsIgnoreCase(tabId)) {
newTab = tabInfo;
}
}
if (newTab == null)
throw new IllegalStateException("Tab NotFind" + tabId);
if (mLastTab != newTab) {
if (ft == null)
ft = mFragmentManager.beginTransaction();
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.hide(mLastTab.fragment);
}
}
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(mContext, newTab.clazz.getName(), newTab.args);
ft.add(mContainerId, newTab.fragment, newTab.tag);
} else {
ft.show(newTab.fragment);
}
mLastTab = newTab;
}
return ft;
}
/**
* fragment的銷燬
*
* @param tabId 需要銷燬fragment的id
*/
public void destory(String tabId) {
TabInfo newTab = null;
for (int i = 0; i < mTabs.size(); i++) {
TabInfo tab = mTabs.get(i);
if (tab.tag.equals(tabId)) {
newTab = tab;
}
}
if (newTab == null) {
throw new IllegalStateException("Tab NotFind" + tabId);
}
if (newTab.fragment != null) {
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.remove(newTab.fragment).commitAllowingStateLoss();
newTab.fragment = null;
}
if (newTab == mLastTab) {
mLastTab = null;
}
}
/**
* 選項卡類
*/
class TabInfo {
private String tag;
private Class<?> clazz;
private Bundle args;
private Fragment fragment;
public TabInfo(String tag, Class<?> clazz, Bundle args) {
this.tag = tag;
this.clazz = clazz;
this.args = args;
}
}
}
- 客戶端程式碼比較,定義需要新增的Fargment的tag,通過FragmentTabManager的addTab( )方法新增Fragment,通過FragmentTabManager中的setOnTabChangedListener( )方法設定選項卡切換監聽,並實現onTabChanged方法,實現比較簡單,就是簡單的比較一下當前選中的tabID跟開始定義的tag。
public class MainActivity extends FragmentActivity {
private FragmentTabManager mFragmentTabManager;
private final String WEDDING_MAIN = "main",
WEDDING_SERVICE = "service",
WEDDING_SHOW = "show",
WEDDING_USER = "user";
private TextView mTv_main, mTv_service, mTv_show, mTv_user;
private View.OnClickListener mOnClickListener=new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.tv_main:
mFragmentTabManager.changeTab(WEDDING_MAIN);
break;
case R.id.tv_service:
mFragmentTabManager.changeTab(WEDDING_SERVICE);
break;
case R.id.tv_show:
mFragmentTabManager.changeTab(WEDDING_SHOW);
break;
case R.id.tv_user:
mFragmentTabManager.changeTab(WEDDING_USER);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv_main = (TextView) findViewById(R.id.tv_main);
mTv_service = (TextView) findViewById(R.id.tv_service);
mTv_show = (TextView) findViewById(R.id.tv_show);
mTv_user = (TextView) findViewById(R.id.tv_user);
mTv_main.setOnClickListener(mOnClickListener);
mTv_service.setOnClickListener(mOnClickListener);
mTv_show.setOnClickListener(mOnClickListener);
mTv_user.setOnClickListener(mOnClickListener);
mFragmentTabManager = new FragmentTabManager(this, getSupportFragmentManager(), R.id.fra_contain);
mFragmentTabManager.addTab(WEDDING_MAIN, MainFragment.class, null)
.addTab(WEDDING_SERVICE, ServiceFragment.class, null)
.addTab(WEDDING_SHOW, ShowFragment.class, null)
.addTab(WEDDING_USER, UserFragment.class, null);
mFragmentTabManager.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
MainActivity.this.onTabChange(tabId);
}
});
if (savedInstanceState == null) {
mFragmentTabManager.changeTab(WEDDING_MAIN);
} else {
String tabId = savedInstanceState.getString("tab");
onTabChange(tabId);
}
}
private void onTabChange(String tabId) {
mTv_main.setSelected(tabId.equals(WEDDING_MAIN));
mTv_service.setSelected(tabId.equals(WEDDING_SERVICE));
mTv_show.setSelected(tabId.equals(WEDDING_SHOW));
mTv_user.setSelected(tabId.equals(WEDDING_USER));
}