考慮到為了減少UI的'刷新效果',EL使用Fragment來(lái)代替常規(guī)的Activity方式.
Fragment代替Activity的好處很多,比如沒(méi)有不同UI間切換的'閃動(dòng)',視覺(jué)是上也有加快顯示的效果.但這種效果也是有代價(jià)的, 大量Activity方式下的工作,在Fragment時(shí),不得不自己來(lái)實(shí)現(xiàn),比如UI的'壓棧'等等.
這里記錄下EL實(shí)現(xiàn)中,碰到的跟Fragment相關(guān)的問(wèn)題. (Fragment相關(guān)文檔的
官方鏈接在這里)
1. 參數(shù)傳遞
Activity切換時(shí),可以在StartActivity()時(shí),通過(guò)Intent的方式將所需參數(shù)傳遞給下一個(gè)Activity. 但在Fragment方式下就不行了,此時(shí)可使用/參考Fragment支持的setArguments()和getArguments(). EL中所有Fragment都繼承于BaseFragment,在顯示Fragment時(shí),使用設(shè)計(jì)的OnArgument()接口傳遞參數(shù).
fragment.onArguments(args);
fragmentManager.beginTransaction().show(fragment).commit();
2. 界面切換
FragmentManager是用于管理Fragment的類,通過(guò)內(nèi)部的FragmentTranscation對(duì)象實(shí)現(xiàn)Fragment的加載/顯示/隱藏/移除等操作. EL實(shí)現(xiàn)中,使用FragmentSwitcher類封裝了FrangmentManager,以實(shí)現(xiàn)某些更靈活的Fragment管理,比如某些Fragment在被切換是需要?jiǎng)h除,而其他的僅需要隱藏等操作.
public enum Type {
LIST("list", false), SHOW("show", false), ABOUT("about", true), SETTING("setting", true),
DOWNLOAD("download", true);
private final String title;
private final boolean removed;
private Type(final String title, boolean removed) {
this.title = title;
this.removed = removed;
}
public String getTitle() {
return title;
}
public boolean hasRemoved() {
return removed;
}
public static Type getType(final String title) {
if (title.equals(LIST.getTitle())) {
return LIST;
} else if (title.equals(SHOW.getTitle())) {
return SHOW;
} else {
return null;
}
}
}
public boolean show(Type type, Bundle args) {
if (curType != null) {
if (curType == type) {
((BaseFragment) fragmentManager.findFragmentByTag(type.getTitle())).onArguments(args);
return true;
} else {
hide(curType);
}
}
BaseFragment fragment = (BaseFragment) fragmentManager.findFragmentByTag(type.getTitle());
if (fragment == null) {
fragment = create(type);
if (fragment == null) {
return false;
}
}
fragment.onArguments(args);
fragmentManager.beginTransaction().show(fragment).commit();
curType = type;
return true;
}
3. Fragment的棧
Activity在切換時(shí),通常情況下可以通過(guò)BACK鍵返回前一個(gè)Activity,是因?yàn)榇嬖谝粋€(gè)'棧'來(lái)存放前面的Activity. Fragment也有棧的概念,但在操作上就需要自己主動(dòng)調(diào)用了 -- Fragment隱藏時(shí),壓棧; Fragment退出時(shí),出棧.
private void hide(Type type) {
BaseFragment fragment = (BaseFragment) fragmentManager.findFragmentByTag(type.getTitle());
if (fragment != null) {
if (type.hasRemoved()) {
fragmentManager.beginTransaction().remove(fragment).commit();
} else {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.addToBackStack(type.getTitle());
ft.hide(fragment);
ft.commit();
// fragmentManager.beginTransaction().hide(fragment).commit();
}
curType = null;
}
}
public boolean showPrevFragment() {
int count = fragmentManager.getBackStackEntryCount();
if (count > 0) {
String name = fragmentManager.getBackStackEntryAt(count - 1).getName();
fragmentManager.popBackStack();// .popBackStackImmediate();
Type type = Type.getType(name);
if (type != null) {
show(type);
return true;
}
}
return false;
}
總的來(lái)說(shuō),Fragment要想用著方便,自己使用FragmentSwitcher和BaseFragment來(lái)封裝下FragmentManager和Fragment還是不錯(cuò)的選擇.
Fragment看起來(lái)很美,用起來(lái)真是發(fā)瘋啊.上面這些都是實(shí)現(xiàn)EL中碰到的問(wèn)題,這里做下記錄,怕回頭又忘記'痛苦地翻文檔'的日子...
另,EL基本完工,這兩天可發(fā)布Alpha版了...