基本实现已经完成了,接下来就是封装了。
一、Acitvity的封装
我把Activity封装成了三层
1.MvvmActivity
public abstract class MvvmActivity<V extends ViewDataBinding, VM extends ViewModel> extends AppCompatActivity {
/**
* DataBinding
*/
protected V mDataBinding;
/**
* ViewModel
*/
protected VM mViewModel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initAAC();
}
/**
* 获取视图ID
*
* @return 视图ID
*/
protected abstract int getLayoutId();
/**
* 获取ViewModel
*
* @return ViewModel
*/
protected abstract VM getViewModel();
/**
* 初始化 DataBinding 变量ID 和 变量实体类 Map
*
* @return DataBinding 变量SparseArray
*/
protected abstract SparseArray<Object> getVariableSparseArray();
/**
* 初始化AAC组件
*/
private void initAAC() {
mDataBinding = DataBindingUtil.setContentView(this, getLayoutId());
mViewModel = getViewModel();
final SparseArray<Object> variableSA = getVariableSparseArray();
if (variableSA != null && variableSA.size() > 0) {
for (int i = 0; i < variableSA.size(); ++i) {
final int variableId = variableSA.keyAt(i);
final Object variableValue = variableSA.valueAt(i);
mDataBinding.setVariable(variableId, variableValue);
}
}
//让ViewModel拥有View的生命周期感应
if (mViewModel != null) {
getLifecycle().addObserver(mViewModel);
}
}
/**
* 工厂模式创建ViewModel
*
* @param cls Activity Class
* @param factory 工厂
* @return ViewModel子类
*/
public VM createViewModel(@NonNull Class<VM> cls, @NonNull ViewModelProvider.NewInstanceFactory factory) {
return new ViewModelProvider(this, factory).get(cls);
}
@Override
protected void onDestroy() {
super.onDestroy();
//销毁DataBinding
if (mDataBinding != null) {
mDataBinding.unbind();
}
}
}
第一层把databinding进行绑定和解绑,给ViewModel注册生命周期感知
2.MvvmActivity
public abstract class MvvmBaseActivity<V extends ViewDataBinding, VM extends ViewModel> extends MvvmActivity<V, VM> {
/**
* RxPermission (响应式权限申请框架)
*/
protected RxPermissions mRxPermissions;
/**
* 页面跳转携带的数据Bundle
*/
protected Bundle mBundle;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initParam();
initData();
initAction();
initObserver();
}
/**
* 初始化参数
*/
protected void initParam() {
if (getIntent() != null) {
mBundle = getIntent().getExtras();
}
}
/**
* 初始化数据
*/
protected void initData() {
//创建RxPermission对象
if (this.getClass().isAnnotationPresent(NeedPermissions.class)) {
mRxPermissions = new RxPermissions(this);
}
//注册EventBus
if (this.getClass().isAnnotationPresent(BindEventBus.class)
&& !EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}
}
/**
* 初始化事件
*/
protected void initAction() {
}
/**
* 初始化 LiveData Observer
*/
protected void initObserver() {
//显示加载中提示对话框
mViewModel.getUiChangeLiveDataManager().getShowLoadingDialogEvent().observe(this, s -> {
LoadingDialog.getInstance()
.setHintText(s)
.show(this);
});
//隐藏加载中提示对话框
mViewModel.getUiChangeLiveDataManager().getHideLoadingDialogEvent().observe(this, o -> {
LoadingDialog.getInstance().hide();
});
//切换Activity
mViewModel.getUiChangeLiveDataManager().getStartActivityEvent().observe(this, paramSA -> {
final Class<?> cls = (Class<?>) paramSA.get(BaseViewModel.ParamConstant.CLASS);
final Bundle bundle = (Bundle) paramSA.get(BaseViewModel.ParamConstant.BUNDLE);
final Integer requestCode = (Integer) paramSA.get(BaseViewModel.ParamConstant.REQUEST_CODE);
if (requestCode == null) {
startActivity(cls, bundle);
} else {
startActivityForResult(cls, bundle, requestCode);
}
});
//关闭销毁Activity
mViewModel.getUiChangeLiveDataManager().getFinishEvent().observe(this, o -> {
finish();
});
}
/**
* 通过Class跳转界面
**/
public void startActivity(Class<?> cls) {
startActivity(cls, null);
}
/**
* 含有Bundle通过Class跳转界面
**/
public void startActivity(Class<?> cls, Bundle bundle) {
Intent intent = new Intent();
intent.setClass(this, cls);
if (bundle != null) {
intent.putExtras(bundle);
}
startActivity(intent);
}
/**
* 通过Class跳转界面
**/
public void startActivityForResult(Class<?> cls, int requestCode) {
startActivityForResult(cls, null, requestCode);
}
/**
* 含有Bundle通过Class跳转界面
**/
public void startActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
Intent intent = new Intent();
intent.setClass(this, cls);
if (bundle != null) {
intent.putExtras(bundle);
}
startActivityForResult(intent, requestCode);
}
@Override
protected void onDestroy() {
super.onDestroy();
//销毁加载中Dialog
LoadingDialog.getInstance().destory();
//销毁权限申请框架
mRxPermissions = null;
//清空Disposable
clearDisposable();
//解注册EventBus
if (this.getClass().isAnnotationPresent(BindEventBus.class)
&& EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().unregister(this);
}
}
}
第二层封装了一些通用的方法和一些必须框架的绑定和解绑
3.MvvmBaseActivity
public abstract class AppBaseActivity<V extends ViewDataBinding, VM extends ViewModel> extends MvvmBaseActivity<V, VM> {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtil.d("页面", this.getClass().getSimpleName());
}
@Override
protected void onStart() {
super.onStart();
initStatusBar();
}
/**
* 初始化数据
*/
@Override
protected void initData() {
super.initData();
//Activity默认背景为白色
getContentView().setBackgroundResource(R.color._FFFFFF);
//设置状态栏高度
if (!isSetCustomStatusBarHeight()) {
@IdRes final int id = ResourceUtil.getResId("rl_layout_title_bar", R.id.class);
if (id != -1) {
final View view = findViewById(id);
if (view != null) {
view.setPadding(0, StatusBarUtil.getStatusBarHeight(this), 0, 0);
}
}
}
}
/**
* 初始化状态栏
*/
private void initStatusBar() {
//设置默认状态栏背景颜色 为透明色
StatusBarUtil.setStatusBarColor(this, R.color._00000000);
//设置默认状态栏字体颜色 为浅色
StatusBarUtil.setStatusBarContentColor(this, isSetStatusBarContentDark());
}
/**
* 是否自定义设置状态栏高度
*
* @return true 自定义设置 false 默认设置
*/
protected boolean isSetCustomStatusBarHeight() {
return false;
}
/**
* 是否设置状态栏内容颜色为深色
*
* @return true 深色 false 浅色
*/
protected boolean isSetStatusBarContentDark() {
return false;
}
/**
* 连续双击退出应用
*/
protected boolean mExitApp;
/**
* 指定间隔时间内 按下两次返回键退出App
*/
public void doubleDownExitApp() {
if (mExitApp) {
//关闭所有的Activity
ActivityUtils.finishAllActivities();
} else {
mExitApp = true;
ToastUtils.showShort("再按一次退出应用");
//超过两秒没有再次点击,则不退出App
new Handler().postDelayed(() -> mExitApp = false, 2000);
}
}
}
第三层封装了一些沉浸式状态栏和双击退出的一些方法
以上利用泛型限制了数据类型,利用了抽象方法让子类实现变化的部分。
二、改造后的DemoActivity
public class DemoActivity extends AppBaseActivity<ActivityDemoBinding, AClass> {
@Override
protected int getLayoutId() {
return R.layout.activity_demo;
}
@Override
protected AClass getViewModel() {
return new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(AClass.class);
}
@Override
protected SparseArray<Object> getVariableSparseArray() {
SparseArray<Object> variableSA = new SparseArray<>();
variableSA.put(aClass, mViewModel);
return variableSA;
}
}
网友评论