安卓开发 第六篇 我的安卓应用架构设计-----BaseActivity类
来源:互联网 发布:跟单软件 编辑:程序博客网 时间:2024/05/18 00:55
BaseActivity是我的架构设计中所有activity的基类,含有一些公共的属性和方法,同时控制toolbar的显示,以及其他一些功能。。。
来看源码:
/** * BaseActivity 是所有activity的基类,把一些公共的方法放到里面 * <p> * Created by tianlai on 16-3-3. */public abstract class BaseActivity extends AppCompatActivity implements BaseUiView { private ActivityComponent activityComponent; @Inject protected LayoutInflater inflater; @Inject protected Context context; @Inject protected AppPageManager appPageManager; protected Toolbar toolbar; protected LinearLayout rootView; protected BasePresenter presenter; protected LoadingDialog loadingDialog; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 使用依赖注入 initInjector(); injectActivity(activityComponent); //设置根布局 rootView = (LinearLayout) inflateView(R.layout.activity_base); setContentView(rootView); // 将当前activity加入ActivityManager中 appPageManager.addActivity(this); // 设置导航栏 toolbar = (Toolbar) find(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayShowTitleEnabled(false); initToolbar(toolbar); //初始化加载数据对话框 loadingDialog=new LoadingDialog(this); loadingDialog.setCanceledOnTouchOutside(false); loadingDialog.setCancelable(true); loadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface loadingDialog) { presenter.cancleLoading(); } }); //初始化Presenter presenter = getPresenter(); if (presenter == null) { throw new NullPointerException("presenter 不能为 Null"); } else { presenter.oncreate(savedInstanceState, context); } } /** * 初始化Toolbar * * @param toolbar */ protected void initToolbar(Toolbar toolbar) { toolbar.setNavigationIcon(R.mipmap.ic_toolbar_navigation); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); } /** * 设置页面的标题 * * @param title */ protected void setPageTitle(String title) { setPageTitle(FontUtil.addColor("#F04877", title)); } /** * @param title */ protected void setPageTitle(CharSequence title) { toolbar.setTitle(title); } /** * @param resId */ protected void setNavigationIcon(int resId) { toolbar.setNavigationIcon(resId); } /** * // 加载ContentView的内容 * * @param view */ protected void setContentViewToRoot(View view) { LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout .LayoutParams.MATCH_PARENT); rootView.addView(view, params); } protected void setContentViewToRoot(int layoutId) { setContentViewToRoot(inflateView(layoutId)); } /** * 进行注入 * * @param activityComponent */ protected abstract void injectActivity(ActivityComponent activityComponent); /** * 获取 ActivityModule * * @return */ protected ActivityModule getActivityModule() { return new ActivityModule(this); } /** * 获取注入Activity的Presenter对象 * * @return */ protected abstract BasePresenter getPresenter(); /** * 隐藏Toolbar */ protected void hideToolbar() { toolbar.setVisibility(View.GONE); } /** * 把布局变成View * * @param layoutId * @return */ protected View inflateView(int layoutId) { return inflater.inflate(layoutId, null); } /** * 设置注入器 */ private void initInjector() { ApplicationComponent appComponent = getAppComponent(); appComponent.inject(this); activityComponent = DaggerActivityComponent.builder() .applicationComponent(appComponent) .activityModule(new ActivityModule(this)) .build(); } /** * 获取 ApplicationComponent * * @return */ public ApplicationComponent getAppComponent() { return ((AppApplication) getApplication()).getAppComponent(); } /** * activityComponent的getter方法 * * @return */ public ActivityComponent getActivityComponent() { return activityComponent; } /** * 显示页面加载状态 */ @Override public void showLoading() { if (loadingDialog != null && !loadingDialog.isShowing()) { loadingDialog.show(); } } /** * 加载完成,隐藏加载状态 */ @Override public void loadingComplete() { if (loadingDialog != null && loadingDialog.isShowing()) { loadingDialog.dismiss(); } } @Override protected void onResume() { super.onResume(); presenter.onResume(this); Bugtags.onResume(this); } @Override protected void onPause() { super.onPause(); presenter.onPause(); Bugtags.onPause(this); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); presenter.onSave(outState); } @Override protected void onDestroy() { super.onDestroy(); appPageManager.removeActivity(this); presenter.onDestroy(); presenter = null; ToastUtil.cancleToast(); } /** * 生成Toolbar右边Action按钮 * * @param name * @param viewId * @return */ @SuppressLint("NewApi") public TextView addRightActionView(CharSequence name, int viewId) { TextView tv_right = new TextView(context); if (viewId == 0) { tv_right.setId(R.id.tv_right_action); } else { tv_right.setId(viewId); } tv_right.setTextColor(getResources().getColor(R.color.font_red)); tv_right.setText(name); tv_right.setCompoundDrawablePadding(DpUtil.dp2px(context, 3)); tv_right.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); tv_right.setGravity(Gravity.CENTER); Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT); layoutParams.rightMargin = DpUtil.dp2px(context, 10); layoutParams.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT; tv_right.setLayoutParams(layoutParams); return tv_right; } /** * 生成Toolbar中心显示titile的View * * @return */ @SuppressLint("NewApi") public TextView addCenterTitleView(String title, int viewId) { TextView tv_title = new TextView(context); if (viewId == 0) { tv_title.setId(R.id.tv_center_title); } else { tv_title.setId(viewId); } tv_title.setTextColor(getResources().getColor(R.color.font_red)); tv_title.setCompoundDrawablePadding(DpUtil.dp2px(context, 3)); tv_title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); tv_title.setGravity(Gravity.CENTER); Toolbar.LayoutParams layoutParams = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT); layoutParams.gravity = Gravity.CENTER; tv_title.setLayoutParams(layoutParams); if (title != null) { tv_title.setText(title); } return tv_title; } /** * 设置应用的字体不随系统设置的更改而更改 */ @Override public Resources getResources() { Resources resources = super.getResources(); Configuration config = new Configuration(); config.setToDefaults(); resources.updateConfiguration(config, resources.getDisplayMetrics()); return resources; } public AppPageManager getAppPageManager() { return appPageManager; } public void setAppPageManager(AppPageManager appPageManager) { this.appPageManager = appPageManager; } /** * 获取view * * @param viewId * @return */ public View find(int viewId) { return findViewById(viewId); } /** * 获取view * * @param viewId * @return */ public View find(View parent, int viewId) { return parent.findViewById(viewId); } /** * 隐藏软键盘 */ protected void hideIME() { ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow( this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); }}
BaseActivity中的有些方法是由于项目的需要而添加的,可能在其他项目中不需要,这点请大家自行忽略。
其中BaseUiView 是所有对view操作接口的基类:
/** * BaseUiView 是应用中所有UiView的顶级抽象类,适合抽取UiView的公共方法和属性 * * UiView:MVP架构中的V。 * * Created by tianlai on 16-3-3. */public interface BaseUiView { /** * showLoading 方法主要用于页面请求数据时显示加载状态 */ public void showLoading(); /** * showEmpty 方法用于请求的数据为空的状态 */ public void showEmpty(); /** * showError 方法用于请求数据出错 */ public void showError(); /** * showError 方法用于请求数据完成 */ public void loadingComplete();}
下面来看一个具体的activity:
/** * LoginActivity app的登录页面 * <p> * Created by tianlai on 16-3-3. */public class LoginActivity extends BaseActivity implements LoginView { @Inject LoginPresenter loginPresenter; @Bind(R.id.edt_account) EditText edtAccount; @Bind(R.id.edt_psw) EditText edtPsw; @Bind(R.id.tv_login) TextView tvLogin; @Bind(R.id.tv_register) TextView tvRegister; @Bind(R.id.tv_forgetpsw) TextView tvForgetpsw; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View contentView = inflateView(R.layout.activity_login); setContentViewToRoot(contentView); ButterKnife.bind(this, contentView); hideToolbar(); } @OnClick({R.id.tv_login,R.id.tv_register,R.id.tv_forgetpsw}) public void OnClick(View v){ switch (v.getId()){ case R.id.tv_login: //进行登录请求 loginPresenter.login(getPhoneNum(),getPasswd()); break; case R.id.tv_register: toRegisterPage(); break; case R.id.tv_forgetpsw: toResetPasswdPage(); break; } } @Override protected void injectActivity(ActivityComponent activityComponent) { activityComponent.inject(this); } @Override protected BasePresenter getPresenter() { return loginPresenter; } @Override public void showEmpty() { } @Override public void showError() { } @Override public void toRegisterPage() { startActivity(new Intent(this,RegisterActivity.class)); } @Override public String getPhoneNum() { return edtAccount.getText().toString().trim(); } @Override public String getPasswd() { return edtPsw.getText().toString().trim(); } @Override public void toMainPage() { startActivity(new Intent(this,MainActivity.class)); finish(); } @Override public void toResetPasswdPage() { startActivity(new Intent(this,ResetPswActivity.class)); }}
而LoginView :
/** * * Created by tianlai on 16-3-3. */public interface LoginView extends BaseUiView { /** * 去到注册页面 */ public void toRegisterPage(); /** * 获取登录手机号 * * @return */ public String getPhoneNum(); /** * 获取登录密码 * * @return */ public String getPasswd(); /** * 去主页面 */ public void toMainPage(); /** * 去重置密码页面 */ public void toResetPasswdPage();}
页面布局(activity_login.xml):
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/bg_login" android:gravity="center" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="用户登录" android:textColor="@color/white" android:textSize="25sp" android:textStyle="bold" /> <EditText android:id="@+id/edt_account" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" android:paddingLeft="8dp" android:paddingRight="8dp" android:maxLength="20" android:singleLine="true" android:textColor="@color/font_black_light" android:textColorHint="@color/hint_color" android:hint="@string/hint_account" android:layout_marginTop="60dp" android:background="@color/white" /> <EditText android:id="@+id/edt_psw" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" android:paddingLeft="8dp" android:singleLine="true" android:paddingRight="8dp" android:maxLength="16" android:textColor="@color/font_black_light" android:textColorHint="@color/hint_color" android:hint="@string/hint_psw" android:layout_marginTop="20dp" android:background="@color/white" /> <TextView android:id="@+id/tv_login" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" android:layout_marginTop="40dp" android:background="@drawable/bg_btn_round_green_5" android:gravity="center" android:text="@string/login_now" android:textColor="@color/whitesmoke" android:textSize="18sp" /> <TextView android:id="@+id/tv_register" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginLeft="40dp" android:layout_marginRight="40dp" android:layout_marginTop="20dp" android:background="@drawable/bg_btn_round_green_5" android:gravity="center" android:text="@string/register_now" android:textColor="@color/whitesmoke" android:textSize="18sp" /> <TextView android:id="@+id/tv_forgetpsw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:gravity="center" android:text="@string/forget_psw" android:textColor="@color/aqua" android:textSize="14sp" /></LinearLayout>
最终效果:
额,发现没什么可以说的了,就到这里吧!
如果有更深的理解,本文将会修改;
如果有错误的地方,欢迎指正;
如果你有更好的理解,欢迎交流。
本文为原创文章,版权归博主所有,转载请注明出处。
更多资料:
我的github地址以及使用demo: https://github.com/naivor/naivorapp
0 0
- 安卓开发 第六篇 我的安卓应用架构设计-----BaseActivity类
- 安卓开发 第三篇 我的安卓应用架构设计-----Responce类
- 安卓开发 第四篇 我的安卓应用架构设计-----Presenter类
- 安卓开发 第五篇 我的安卓应用架构设计-----Repository类
- 安卓开发 第二篇 我的安卓应用架构设计
- 安卓开发 第八篇 我的安卓应用架构设计-----图片选择以及剪裁
- 安卓开发 第七篇 我的安卓应用架构设计-----Adapter类及ViewHolder类
- 第二篇 我的安卓应用架构设计
- 安卓开发基类BaseActivity、BaseApplication
- 安卓日记——设计一个通用的BaseActivity
- 安卓开发中Activity抽象基类BaseActivity的编写
- 安卓设计架构
- 我的安卓开发学习笔记一:安卓系统架构
- 提高安卓APP开发效率、管理代码的架子(一):BaseActivity&BaseApplication
- 手把手教你如何搭建一个自己的安卓快速开发框架之BaseActivity(一)
- 安卓应用架构思考
- 【安卓】安卓App开发思路 一步一个脚印(三)BaseActivity BaseFragment
- 安卓的架构
- java异常处理相关
- EventBus使用详解(二)
- SDRAM 命名规则
- IOS开发之手势——UIGestureRecognizer 共存
- 场效应管的记法
- 安卓开发 第六篇 我的安卓应用架构设计-----BaseActivity类
- 3D图形学(一):三维几何学基础(1):三维坐标系
- teamviewer 打不开 提示:wine client error:0: version mismatch 440/438.
- 通过sqli-labs学习sql注入——基础挑战之less11-22
- C++注释规范
- JS仿黑客帝国字母掉落效果
- Android性能优化 一 优化小结
- 对SqliteDatabase.findEditTable的改进
- Java自带的性能监测工具用法简介——jstack、jconsole、jinfo、jmap、jdb、jsta、jvisualvm