这篇文章是我个人在开发与设计APP的所用的一些设计思路,仅用于和大家分享知识并不是什么设计标准之类的。主要说明App的开发中是如何来开发与组织UI部分。
UI模块结构
在项目中建立ui
包用于存放ui
类文件。这是第一步第二步是建立相应的组件包名: activity
、fragment
、widget
、adapter
、widget
等以上是最基本的组件现在ui
结构如下:
基类设计
base
存放ui
通用的部分,结构如下:
UIDelegate
定义一些常用的UI操作如: Toast
、Dialog
、获取Resources
等功能。
public interface UIDelegate {
Toast makeToast(String msg, int len);
void showToastShort(String msg);
void showToastLong(String msg);
void showToastShort(@StringRes int msg);
void showToastLong(@StringRes int msg);
Dialog showWaitDialog(@StringRes int msg);
Dialog showWaitDialog(String msg);
Dialog showWaitDialog(@NonNull String msg, boolean cancelable, @Nullable DialogInterface.OnCancelListener callback);
Dialog showWaitDialog(@StringRes int msg, boolean cancelable, @Nullable DialogInterface.OnCancelListener callback);
void dismissWaitDialog();
}
BaseActivity
继承Activity
实现最基本的功能如打印生命周期
、获取参数
等BaseActivity
实现UIDelegate
提供最基本的Toast
与Dialog
显示功能部分代码如下:
public abstract class BaseActivity extends AppCompatActivity implements UIDelegate{
private static final String ACTIVITY_LIFE_CYCLE_TAG = "activity_life_cycle";
private static final String LOG_FORMAT = "%s: %s";
private ProgressDialog mWaitDialog;
// activity life cycle start
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LianLianLog.d(ACTIVITY_LIFE_CYCLE_TAG, LOG_FORMAT, this.getLocalClassName(), "onCreate");
ActivityStack.getActivityStack().addActivity(this);
}
@SuppressWarnings("unchecked")
public <T> T getExtra(String key, T defaultValue){
Bundle bundle = getIntent().getExtras();
if(bundle != null && bundle.containsKey(key)){
return (T) bundle.get(key);
}
return defaultValue;
}
@SuppressWarnings("unchecked")
public <T> T getExtra(String key){
Bundle bundle = getIntent().getExtras();
if(bundle != null && bundle.containsKey(key)){
return (T) bundle.get(key);
}
return null;
}
@Override
public Toast makeToast(String msg, int len) {
if(TextUtils.isEmpty(msg))
return null;
return Toast.makeText(this, msg, len);
}
@Override
public void showToastShort(String msg) {
Toast toast = makeToast(msg, Toast.LENGTH_SHORT);
if(toast != null)
toast.show();
}
@Override
public void showToastLong(String msg) {
makeToast(msg, Toast.LENGTH_LONG).show();
Toast toast = makeToast(msg, Toast.LENGTH_LONG);
if(toast != null)
toast.show();
}
@Override
public void showToastShort(@StringRes int msg) {
showToastShort(getString(msg));
}
@Override
public void showToastLong(@StringRes int msg) {
showToastLong(getString(msg));
}
@Override
public Dialog showWaitDialog(@StringRes int msg) {
return showWaitDialog(getString(msg));
}
@Override
public Dialog showWaitDialog(String msg) {
return showWaitDialog(msg, false, null);
}
@Override
public Dialog showWaitDialog(@NonNull String msg, boolean cancelable, @Nullable DialogInterface.OnCancelListener callback) {
if(mWaitDialog == null) {
mWaitDialog = ProgressDialog.show(this, null, msg, true, cancelable, callback);
mWaitDialog.setCanceledOnTouchOutside(false);
}else{
mWaitDialog.setMessage(msg);
mWaitDialog.show();
}
return mWaitDialog;
}
@Override
public Dialog showWaitDialog(@StringRes int msg, boolean cancelable, @Nullable DialogInterface.OnCancelListener callback) {
return showWaitDialog(getString(msg), cancelable, callback);
}
@Override
public void dismissWaitDialog() {
if(mWaitDialog != null)
mWaitDialog.dismiss();
mWaitDialog = null;
}
}
BaseFragment
同样也实现UIDelegate
接口但是他的功能其实是由BaseActivity
去完成的部分代码如下:
public class BaseFragment extends Fragment implements UIDelegate {
private static final String FRAGMENT_LIFE_CYCLE_TAG = "fragment_life_cycle";
private static final String LOG_FORMAT = "%s: %s";
private UIDelegate mDelegate;
@Override
public void onAttach(Context context) {
super.onAttach(context);
LianLianLog.d(FRAGMENT_LIFE_CYCLE_TAG, LOG_FORMAT, getLogTag(), "onAttach");
if(context instanceof UIDelegate)
mDelegate = (UIDelegate) context;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LianLianLog.d(FRAGMENT_LIFE_CYCLE_TAG, LOG_FORMAT, getLogTag(), "onActivityCreated");
}
@SuppressWarnings("unchecked")
public <T> T getExtra(String key, T defaultValue){
Bundle bundle = getArguments();
if(bundle != null && bundle.containsKey(key)){
return (T) bundle.get(key);
}
return defaultValue;
}
@SuppressWarnings("unchecked")
public <T> T getExtra(String key){
Bundle bundle = getArguments();
if(bundle != null && bundle.containsKey(key)){
return (T) bundle.get(key);
}
return null;
}
private String getLogTag() {
return getTag() != null ? getTag() : getClass().getSimpleName();
}
@Override
public Toast makeToast(String msg, int len) {
return mDelegate != null ? mDelegate.makeToast(msg, len) : null;
}
@Override
public void showToastShort(String msg) {
if(mDelegate != null)
mDelegate.showToastShort(msg);
}
@Override
public void showToastLong(String msg) {
if(mDelegate != null)
mDelegate.showToastLong(msg);
}
@Override
public void showToastShort(@StringRes int msg) {
if(mDelegate != null)
mDelegate.showToastShort(msg);
}
@Override
public void showToastLong(@StringRes int msg) {
if(mDelegate != null)
mDelegate.showToastLong(msg);
}
@Override
public Dialog showWaitDialog(@StringRes int msg) {
return mDelegate != null ? mDelegate.showWaitDialog(msg) : null;
}
@Override
public Dialog showWaitDialog(String msg) {
return mDelegate != null ? mDelegate.showWaitDialog(msg) : null;
}
@Override
public Dialog showWaitDialog(@NonNull String msg, boolean cancelable, @Nullable DialogInterface.OnCancelListener callback) {
return mDelegate != null ? mDelegate.showWaitDialog(msg, cancelable, callback) : null;
}
@Override
public Dialog showWaitDialog(@StringRes int msg, boolean cancelable, @Nullable DialogInterface.OnCancelListener callback) {
return mDelegate != null ? mDelegate.showWaitDialog(msg, cancelable, callback) : null;
}
@Override
public void dismissWaitDialog() {
if(mDelegate != null)
mDelegate.dismissWaitDialog();
}
}
UIConstant
定义Activity
或Fragment
通信时所用到的一些常量参数。
public class UIConstant {
String EXTRA_NAME = "extra_name";
String EXTRA_PHONE = "extra_phone";
}
UIHelper
包含一系列的辅助方法如在多人开发过程中你写了一个Activity
但是要启动这个 Activity
必需要带一些参数由于是个人开发的别人也不清楚如何去调用这里推荐在UIHelper
中声明如何去启动这个Activity
。
public class UIHelper {
public static void startInfoActivity(Context context,String phone){
Intent intent = new Intent(context, InfoActivity.class);
intent.putExtra(UIConstant.EXTRA_PHONE, phone);
intent.setPackage(context.getPackageName());
context.startActivity(intent);
}
public static boolean sdkCompare(int sdkInt){
return Build.VERSION.SDK_INT >= sdkInt;
}
public static boolean isDestroyed(Activity activity){
if(UIHelper.sdkCompare(Build.VERSION_CODES.JELLY_BEAN_MR1)){
return activity.isDestroyed();
}
return activity.isFinishing();
}
}
使用MVP
使用MVP
开发模式隔离业务与模型减少View
的代码。让View
实现UIDelegate
这样Presenter
具备显示Toast
、Dialog
等功能。
public interface IView extends UIDelegate{
}