單Activity+多Fragment設(shè)計(jì)(Fragmentation的使用)
本文介紹使用Fragmentation庫(kù)打造單Activity+多Fragment的App基礎(chǔ)架構(gòu):
Fragmentation庫(kù)提供了SupportActivity和SupportFragment兩個(gè)基礎(chǔ)類准验,通過(guò)分別繼承這兩個(gè)類遵馆,實(shí)現(xiàn)自己的業(yè)務(wù)尚困。
-
Fragmentation的Github鏈接
點(diǎn)此鏈接到Github
- 導(dǎo)入依賴
// 在App級(jí)別的gradle中引入Fragment依賴
api 'me.yokeyword:fragmentation:1.2.7'
api 'me.yokeyword:fragmentation-swipeback:1.2.7'
-
封裝BaseFragment
自定義BaseFragment繼承自Fragmentation提供的SwipeBackFragment
將BaseFragment改為抽象類饺谬,在具體業(yè)務(wù)中定義具體的頁(yè)面Fragment時(shí)就繼承這個(gè)類文判,實(shí)現(xiàn)它的抽象方法习劫,傳入布局忽肛,綁定視圖审轮。
public abstract class BaseFragment extends SwipeBackFragment{
// 需要實(shí)現(xiàn)的設(shè)置layout的抽象方法
public abstract setLayout();
// 需要實(shí)現(xiàn)的綁定完界面之后的操作方法
public abstract onBindView(@Nullable Bundle savedInstanceState, View rootView);
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
View rootView = null;
if (setLayout() instanceof Integer){
rootView = inflater.inflate((Integer)setLayout(), container, false);
}else if(setLayout() instanceof View){
rootView = (View)setLayout();
}else{
throw new ClassCastException("type must be int or view");
}
onBindView(@Nullable savedInstanceState, View rootView);
}
// 返回唯一的Activity實(shí)例
public final ProxyActivity getProxyActivity(){
return (ProxyActivity)_mActivity;
}
}
-
封裝ProxyActivity
自定義ProxyActivity繼承自Fragmentation提供的SupportActivity
同樣也是抽象類芥被,用來(lái)綁定自定義的Fragement到界面上
public abstract class ProxyActivity extends SupportActivity{
// 需要實(shí)現(xiàn)的設(shè)置Fragment的方法
public abstract BaseFragment setRootFragment();
@override
protected void onCreate(@Nullable Bundle saveInstanceState){
super.onCreate(saveInstanceState);
initContainer(saveInstanceState);
}
private void initContainer(@Nullable Bundle saveInstanceState){
final ContentFrameLayout container = new ContentFrameLayout(this);
// id要在res/vlues定義一個(gè)資源文件ids欧宜,再定義一個(gè)id值
container.setId(R.id.fragment_container);
setContentView(container);
if (saveInstancesState==null){
// 框架提供的綁定Fragment到Framelayout的方法
loadRootFragment(R.id.fragment_container, setRootFragment());
}
}
// 釋放一些資源
@Override
protected void onDestroy() {
super.onDestroy();
System.gc();
System.runFinalization();
}
}
- 定義具體的MyFragment繼承自BaseFragment
public class MyFragment extends BaseFragment{
@override
public Object setLayout(){
return R.layout.fragment_my;
}
@override
public void onBindView(@Nullable Bundle savedInstanceState, View rootView){
}
}
- 把定義的MyFragment嵌入唯一的MyActivity實(shí)例
public class MyActivity extends ProxyActivity{
@override
public BaseFragment setRootFragment(){
return new MyFragment();
}
}
- 其中Fragmentation中提供的跳轉(zhuǎn)方法
裝載根Fragment,即Activity內(nèi)的第一個(gè)Fragment
loadRootFragment(int containerId, SupportFragment toFragment)
同級(jí)Fragment場(chǎng)景下的切換(類似底部導(dǎo)航欄之間切換Fragment)
showHideFragment(SupportFragment showFragment, SupportFragment hideFragment);
啟動(dòng)Fragment的方法
// 啟動(dòng)新的Fragment,啟動(dòng)者和被啟動(dòng)者是在同一個(gè)棧的
start(SupportFragment fragment)
// 以某種啟動(dòng)模式撕彤,啟動(dòng)新的Fragment
start(SupportFragment fragment, int launchMode)
// 啟動(dòng)新的Fragment鱼鸠,并能接收到新Fragment的數(shù)據(jù)返回
startForResult(SupportFragment fragment,int requestCode)
// 啟動(dòng)目標(biāo)Fragment,并關(guān)閉當(dāng)前Fragment
startWithPop(SupportFragment fragment)
小結(jié)
這樣就可以定義多個(gè)自己業(yè)務(wù)的Fragment繼承自BaseFragment,然后調(diào)用Fragmentation提供的方法羹铅,在唯一的實(shí)例Activity中進(jìn)行跳轉(zhuǎn)蚀狰。基于這個(gè)框架的仿電商導(dǎo)航欄,多Fragment切換
點(diǎn)此鏈接到下一篇文章