Android中封装通用页面逻辑,实现结构化,后期维护方便,可识别度高。
- 根目录下新建activity目录;
- 新建BaseActivity,继承自 AppCompatActivity,表示所有Activity的父类;
package com.ixuea.courses.mymusic.activity;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
/**
* 所有Activity父类
*/
public class BaseActivity extends AppCompatActivity {
/**
* 找控件
*/
protected void initViews(){
}
/**
* 设置数据
*/
protected void initDatum() {
}
/**
* 设置监听器
*/
protected void initListeners() {
}
/**
* 在onCreate方法后面调用
* @param savedInstanceState
*/
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
initViews();
initDatum();
initListeners();
}
}
- 新建 BaseCommonActivity,继承自 BaseActivity,表示通用界面逻辑;
package com.ixuea.courses.mymusic.activity;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Parcelable;
import android.text.TextUtils;
import android.view.Window;
import com.ixuea.courses.mymusic.util.Constant;
/**
* 通用界面逻辑
*/
public class BaseCommonActivity extends BaseActivity{
/**
* 启动界面
*
* @param clazz
*/
protected void startActivity(Class<? extends Activity> clazz) {
startActivity(new Intent(this, clazz));
}
/**
* 启动界面并关闭当前界面
*
* @param clazz
*/
protected void startActivityAfterFinishThis(Class<? extends Activity> clazz) {
startActivity(clazz);
finish();
}
/**
* 启动界面,可以传递一个字符串参数
*
* @param clazz
* @param id
*/
protected void startActivityExtraId(Class<?> clazz, String id) {
//创建Intent
Intent intent = new Intent(this, clazz);
//传递数据
if (!TextUtils.isEmpty(id)) {
//不为空才传递
intent.putExtra(Constant.ID, id);
}
//启动界面
startActivity(intent);
}
/**
* 启动界面,可以传递一个对象参数
*
* @param clazz
* @param data Android sdk提供的一个接口,用于实现数据序列化 任何想要通过 Intent 传递的对象都需要实现这个接口
*/
protected void startActivityExtraData(Class<?> clazz, Parcelable data) {
Intent intent = new Intent(this, clazz);
intent.putExtra(Constant.DATA, data);
startActivity(intent);
}
/**
* 获取字符串
*
* @param key
* @return
*/
protected String extraString(String key) {
return getIntent().getStringExtra(key);
}
protected int extraInt(String key) {
return getIntent().getIntExtra(key, -1);
}
protected <T> T extraData() {
return getIntent().getParcelableExtra(Constant.DATA);
}
/**
* 获取字符串类型Id
*
* @return
*/
protected String extraId() {
return extraString(Constant.ID);
}
/**
* 设置状态栏颜色
*
* @param data
*/
protected void setStatusBarColor(int data) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//设置状态栏颜色
Window window = getWindow();
window.setStatusBarColor(data);
//设置导航栏颜色
window.setNavigationBarColor(data);
}
}
}
- 如下同上,分别是不同页面的逻辑结构,按照项目规划,自行调整即可;
package com.ixuea.courses.mymusic.activity;
import android.view.View;
import com.ixuea.courses.mymusic.R;
import com.ixuea.courses.mymusic.util.PreferenceUtil;
import com.ixuea.courses.mymusic.util.SuperDarkUtil;
import com.ixuea.courses.mymusic.view.PlaceholderView;
import com.ixuea.superui.loading.SuperRoundLoadingDialogFragment;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import java.lang.ref.WeakReference;
/**
* 项目中特有逻辑
*
* 例如:显示迷你控制栏播放状态
*/
public class BaseLogicActivity extends BaseCommonActivity{
protected PreferenceUtil sp;
private WeakReference<SuperRoundLoadingDialogFragment> loadingWeakReference;
private PlaceholderView placeholderView;
// private GlobalLyricManagerImpl globalLyricManager;
@Override
protected void initViews() {
super.initViews();
if (SuperDarkUtil.isDark(this)) {
//状态栏文字白色
QMUIStatusBarHelper.setStatusBarDarkMode(this);
} else {
//状态栏文字黑色
QMUIStatusBarHelper.setStatusBarLightMode(this);
}
}
@Override
protected void initDatum() {
super.initDatum();
sp = PreferenceUtil.getInstance(getHostActivity());
}
/**
* 获取界面方法
*
* @return
*/
protected BaseLogicActivity getHostActivity() {
return this;
}
/**
* 显示加载对话框
*/
public void showLoading() {
showLoading(getString(R.string.loading));
}
/**
* 显示加载对话框
*/
public void showLoading(int data) {
showLoading(getString(data));
}
/**
* 显示加载对话框
*/
public void showLoading(String message) {
if (loadingWeakReference == null || loadingWeakReference.get() == null) {
loadingWeakReference = new WeakReference<>(
SuperRoundLoadingDialogFragment.newInstance(message)
);
}
SuperRoundLoadingDialogFragment dialog = loadingWeakReference.get();
if (dialog.getDialog() == null || !dialog.getDialog().isShowing()) {
dialog.show(getSupportFragmentManager(), "SuperRoundLoadingDialogFragment");
}
}
/**
* 隐藏加载对话框
*/
public void hideLoading() {
SuperRoundLoadingDialogFragment dialog = loadingWeakReference.get();
if (dialog != null) {
dialog.dismiss();
loadingWeakReference.clear();
}
loadingWeakReference = null;
}
/**
* 加载数据方法
*
* @param isPlaceholder 是否是通过placeholder控件触发的
*/
protected void loadData(boolean isPlaceholder) {
}
/**
* 加载数据方法
*/
protected void loadData() {
loadData(false);
}
/**
* 获取当前界面占位view
*
* @return
*/
public PlaceholderView getPlaceholderView() {
if(placeholderView == null){
placeholderView = findViewById(R.id.placeholder);
placeholderView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadData(true);
}
});
}
return placeholderView;
}
}
package com.ixuea.courses.mymusic.activity;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.viewbinding.ViewBinding;
import com.ixuea.courses.mymusic.R;
/**
* 通用标题界面
*/
public class BaseTitleActivity<VB extends ViewBinding> extends BaseViewModelActivity<VB>{
private Toolbar toolbar;
@Override
protected void initViews() {
super.initViews();
toolbar = findViewById(R.id.toolbar);
//初始化Toolbar
setSupportActionBar(toolbar);
//是否显示返回按钮
if (isShowBackMenu()) {
showBackMenu();
}
}
/**
* 是否显示返回按钮
*
* @return
*/
protected boolean isShowBackMenu() {
return true;
}
/**
* 显示返回按钮
*/
protected void showBackMenu() {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Toolbar返回按钮点击
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
}
package com.ixuea.courses.mymusic.activity;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;
import com.ixuea.superui.reflect.ReflectUtil;
/**
* 通用ViewModel Activity
* 包括ViewBinding,主要是处理每次要setContentView
* 以及自动创建ViewModel
* 以及viewModel的通用观察处理
*/
public class BaseViewModelActivity<VB extends ViewBinding> extends BaseLogicActivity {
protected VB binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//调用inflate方法,创建viewBinding
binding = ReflectUtil.newViewBinding(getLayoutInflater(), this.getClass());
setContentView(binding.getRoot());
}
}