APP设计与开发(ui篇)

这篇文章是我个人在开发与设计APP的所用的一些设计思路,仅用于和大家分享知识并不是什么设计标准之类的。主要说明App的开发中是如何来开发与组织UI部分。

UI模块结构

在项目中建立ui包用于存放ui类文件。这是第一步第二步是建立相应的组件包名: activityfragmentwidgetadapterwidget等以上是最基本的组件现在ui结构如下:

APP设计与开发(ui篇)_第1张图片

基类设计

base存放ui通用的部分,结构如下:

APP设计与开发(ui篇)_第2张图片

UIDelegate

定义一些常用的UI操作如: ToastDialog获取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提供最基本的ToastDialog显示功能部分代码如下:

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

定义ActivityFragment通信时所用到的一些常量参数。

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具备显示ToastDialog等功能。

public interface IView extends UIDelegate{

}

你可能感兴趣的:(APP设计与开发(ui篇))