forked from YoKeyword/Fragmentation
-
Notifications
You must be signed in to change notification settings - Fork 52
1、如何开始
薛瑄 edited this page Mar 3, 2020
·
2 revisions
1. 项目下app的build.gradle中依赖:
// appcompatx 是必须的
// 如果想继承SupportActivity/SupportFragment,需要依赖:
implementation 'me.xuexuan:fragmentationx:{最新版}'
// 如果不想继承SupportActivity/SupportFragment,而是通过实现接口,自己定制Support,可仅依赖:
implementation 'me.xuexuan:fragmentationx-core:{最新版}'
// 如果想使用SwipeBack 滑动边缘退出Fragment/Activity功能,完整的添加规则如下:
implementation 'me.xuexuan:fragmentationx:{最新版}'
// swipeback基于fragmentation, 如果是自定制SupportActivity/Fragment,则参照SwipeBackActivity/Fragment实现即可
implementation 'me.xuexuan:fragmentationx-swipeback:{最新版}'
Activity继承SupportActivity:
public class MainActivity extends SupportActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...);
// 栈视图等功能,建议在Application里初始化
Fragmentation.builder()
// 显示悬浮球 ; 其他Mode:SHAKE: 摇一摇唤出 NONE:隐藏
.stackViewMode(Fragmentation.BUBBLE)
.debug(BuildConfig.DEBUG)
...
.install();
if (findFragment(HomeFragment.class) == null) {
loadRootFragment(R.id.fl_container, HomeFragment.newInstance()); // 加载根Fragment
}
}
Fragment继承SupportFragment:
public class HomeFragment extends SupportFragment {
private void xxx() {
// 启动新的Fragment, 另有start(fragment,SINGTASK)、startForResult、startWithPop等启动方法
start(DetailFragment.newInstance(HomeBean));
// ... 其他pop, find, 设置动画等等API, 请自行查看WIKI
}
}
// v1.0.0开始,不强制继承SupportActivity/SupportFragment,可使用接口+委托形式来实现自己的SupportActivity/SupportFragment
实现 ISupportActivity:
public class MySupportActivity extends AppCompatActivity implements ISupportActivity{
final SupportActivityDelegate mDelegate = new SupportActivityDelegate(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDelegate.onCreate(savedInstanceState);
setContentView(...);
// 栈视图等功能,建议在Application里初始化
Fragmentation.builder()
// 显示悬浮球 ; 其他Mode:SHAKE: 摇一摇唤出 NONE:隐藏
.stackViewMode(Fragmentation.BUBBLE)
.debug(BuildConfig.DEBUG)
...
.install();
if (findFragment(HomeFragment.class) == null) {
loadRootFragment(R.id.fl_container, HomeFragment.newInstance()); // 加载根Fragment
}
}
//实现ISupportActivity的接口
//参考 项目的SupportActivity类,添加自己需要的方法
}
实现 ISupportFragment :
public class MySupportFragment extends Fragment implements ISupportFragment {
final SupportFragmentDelegate mDelegate = new SupportFragmentDelegate(this);
//注意类型,最好与项目中implements ISupportActivity 的类相同。因为MySupportActivity 中会复写Activity的一些方法(例如:onBackPressed())
protected MySupportActivity _mActivity;
//实现ISupportActivity的接口
@Override
public void onAttach(Context context) {
super.onAttach(context);
mDelegate.onAttach();
_mActivity = (MySupportActivity)mDelegate.getActivity();
}
···
//参考 项目的SupportActivity类,添加自己需要的方法
}