-
Notifications
You must be signed in to change notification settings - Fork 2.1k
2. API
YoKey edited this page Feb 16, 2017
·
36 revisions
-
loadRootX()
系列方法,操作的对象是 孩子Fragment,一般在saveInstanceState==null时load; -
startX()
,popX()
,find/getX()
系列方法,操作的对象是 兄弟Fragment; -
popChildX()
,find/getChildX()
系列方法,操作的对象是 孩子Fragment。
通过上面3个概念配合详细的API以及栈视图,就可以轻松开发出单Activity+多Fragment结构的App。
Fragmentation.builder()
// 设置 栈视图 模式为 悬浮球模式 SHAKE: 摇一摇唤出 NONE:隐藏
.stackViewMode(Fragmentation.BUBBLE)
// ture时,遇到异常:"Can not perform this action after onSaveInstanceState!"时,会抛出
// false时,不会抛出,会捕获,可以在handleException()里监听到
.debug(BuildConfig.DEBUG)
// 在debug=false时,即线上环境时,上述异常会被捕获并回调ExceptionHandler
.handleException(new ExceptionHandler() {
@Override
public void onException(Exception e) {
// 建议在该回调处上传至我们的Crash监测服务器
// 以Bugtags为例子: 手动把捕获到的 Exception 传到 Bugtags 后台。
// Bugtags.sendException(e);
}
})
.install();
// 查看栈视图Dialog
showFragmentStackHierarchyView();
// 打印查看栈视图Log
logFragmentStackHierarchy(TAG);
// 当Fragment根布局没有设定background属性时,
// Fragmentation默认使用Theme的android:windowbackground作为Fragment的背景,
// 可以通过该方法改变Fragment背景。
setDefaultFragmentBackground(@DrawableRes int backgroundRes);
// 监控其下所有Fragments,一旦Fragment的生命周期(包括库的support生命周期 onSupportVisible等等)被回调,
// 即会调用FragmentLifecycleCallbacks对应的方法
registerFragmentLifecycleCallbacks(FragmentLifecycleCallbacks callback)
1、装载根Fragment,一般在saveInstanceState==null时load
// 装载根Fragment, 即Activity内的第一个Fragment 或 Fragment内的第一个子Fragment
loadRootFragment(int containerId, SupportFragment toFragment)
// 以replace方式装载根Fragment,使用场景见Demo的ShopFragment
replaceLoadRootFragment(int containerId, SupportFragment toFragment, boolean addToBack);
// 装载多个根Fragment,用于同级Fragment的场景,详情见新Demo的MainActivity
loadMultipleRootFragment(int containerId, int showPosition, SupportFragment... toFragments);
附:同级Fragment场景下的切换
// show一个Fragment,hide一个Fragment; 主要用于类似微信主页那种 切换tab的情况
showHideFragment(SupportFragment showFragment, SupportFragment hideFragment);
2、启动Fragment
如果在Activity使用,则本质是FragmentActivity.getSupportFragmentManager().getTopFragment().start(f)
;
如果在Fragment中使用,则是Fragment.getFragmentManager().start(f)
;
// 启动新的Fragment,启动者和被启动者是在同一个栈的
start(SupportFragment fragment)
// 以某种启动模式,启动新的Fragment
start(SupportFragment fragment, int launchMode)
// 启动新的Fragment,并能接收到新Fragment的数据返回
startForResult(SupportFragment fragment,int requestCode)
// 启动目标Fragment,并关闭当前Fragment;不要尝试pop()+start(),动画会有问题
startWithPop(SupportFragment fragment)
// 0.8 New: 你可以使用transaction() + start() 来实现上面的各种startXX()设置更多功能
supportFragment.transaction()
.setTag(tag) // 自定义tag
.addSharedElement(xx).setLaunchMode(SINGLETASK).withPop(true).forResult(1)
.commit();
start(supportFragment); // 接着执行start()
下面的方法是SupportFragment才有的:
// replace方式启动目标Fragment,配合replaceLoadRootFragment()使用
replaceFragment(SupportFragment toFragment, boolean addToBack)
3、出栈
// 出栈当前Fragment(在当前Fragment所在栈内pop)
pop();
// 出栈某一个Fragment栈内之上的Fragments
popTo(Class fragmentClass/String tag, boolean includeSelf);
// 如果想出栈后,紧接着.beginTransaction()开始一个新事务,请使用下面的方法:
// 防止多事务连续执行的异常
popTo(Class fragmentClass, boolean includeSelf, Runnable afterTransaction)
下面的方法是SupportFragment才有的,操作目标是子Fragment:
popChild();
popToChild(Class fragmentClass, boolean includeSelf);
popToChild(Class fragmentClass, boolean includeSelf, Runnable afterTransaction)
4、查找Fragment
// 获取所在栈内的栈顶Fragment
getTopFragment();
// 获取当前Fragment所在栈内的前一个Fragment
getPreFragment();
// 获取所在栈内的某个Fragment
findFragment(Class fragmentClass/String tag);
下面的方法是SupportFragment才有的,从子栈内查找:
getTopChildFragment();
findChildFragment(Class fragmentClass);
5、在onCreateView中立即执行start/pop类方法
如果你需要在Fragment的onCreateView/onActivityCreated里立刻执行start/pop类方法,你需要放入enQueueAction的Runnable里执行:
enQueueAction(Runnable);
6、输入法相关
因为Fragment被出栈时,不会自动隐藏软键盘,以及弹出软键盘有些麻烦,故提供下面2个方法
// 隐藏软键盘 一般用在hide时
hideSoftInput();
// 显示软键盘,调用该方法后,会在onPause时自动隐藏软键盘
showSoftInput(View view);