單Activity+多Fragment設計(Fragmentation的使用)
本文介紹使用Fragmentation庫打造單Activity+多Fragment的App基礎(chǔ)架構(gòu):
Fragmentation庫提供了SupportActivity和SupportFragment兩個基礎(chǔ)類,通過分別繼承這兩個類,實現(xiàn)自己的業(yè)務。
-
Fragmentation的Github鏈接
點此鏈接到Github
- 導入依賴
// 在App級別的gradle中引入Fragment依賴
api 'me.yokeyword:fragmentation:1.2.7'
api 'me.yokeyword:fragmentation-swipeback:1.2.7'
-
封裝BaseFragment
自定義BaseFragment繼承自Fragmentation提供的SwipeBackFragment
將BaseFragment改為抽象類,在具體業(yè)務中定義具體的頁面Fragment時就繼承這個類,實現(xiàn)它的抽象方法,傳入布局,綁定視圖。
public abstract class BaseFragment extends SwipeBackFragment{
// 需要實現(xiàn)的設置layout的抽象方法
public abstract setLayout();
// 需要實現(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實例
public final ProxyActivity getProxyActivity(){
return (ProxyActivity)_mActivity;
}
}
-
封裝ProxyActivity
自定義ProxyActivity繼承自Fragmentation提供的SupportActivity
同樣也是抽象類,用來綁定自定義的Fragement到界面上
public abstract class ProxyActivity extends SupportActivity{
// 需要實現(xiàn)的設置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定義一個資源文件ids,再定義一個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實例
public class MyActivity extends ProxyActivity{
@override
public BaseFragment setRootFragment(){
return new MyFragment();
}
}
- 其中Fragmentation中提供的跳轉(zhuǎn)方法
裝載根Fragment,即Activity內(nèi)的第一個Fragment
loadRootFragment(int containerId, SupportFragment toFragment)
同級Fragment場景下的切換(類似底部導航欄之間切換Fragment)
showHideFragment(SupportFragment showFragment, SupportFragment hideFragment);
啟動Fragment的方法
// 啟動新的Fragment,啟動者和被啟動者是在同一個棧的
start(SupportFragment fragment)
// 以某種啟動模式,啟動新的Fragment
start(SupportFragment fragment, int launchMode)
// 啟動新的Fragment,并能接收到新Fragment的數(shù)據(jù)返回
startForResult(SupportFragment fragment,int requestCode)
// 啟動目標Fragment,并關(guān)閉當前Fragment
startWithPop(SupportFragment fragment)
小結(jié)
這樣就可以定義多個自己業(yè)務的Fragment繼承自BaseFragment,然后調(diào)用Fragmentation提供的方法,在唯一的實例Activity中進行跳轉(zhuǎn)。基于這個框架的仿電商導航欄,多Fragment切換
點此鏈接到下一篇文章