APP设计与开发(ui篇)
這篇文章是我個人在開發與設計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{
}
《架構文摘》每天一篇架構領域重磅好文,涉及一線互聯網公司應用架構(高可用、高性 能、高穩定)、大數據、機器學習等各個熱門領域。
總結
以上是生活随笔為你收集整理的APP设计与开发(ui篇)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数织等逻辑游戏网站推荐
- 下一篇: 三星官宣新机:F54 5G将于6月6日发