美文网首页
Android从零搭建项目01 -BaseActivity

Android从零搭建项目01 -BaseActivity

作者: 伊泽瑞额 | 来源:发表于2019-05-10 10:24 被阅读0次
    • 依赖引入
        implementation 'com.google.code.gson:gson:2.8.0'
        implementation 'com.squareup.okhttp3:okhttp:3.4.1'
        implementation 'com.squareup.retrofit2:retrofit:2.3.0'
        implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
        implementation 'io.reactivex.rxjava2:rxjava:2.1.5'
        //ConverterFactory的String依赖包
        implementation 'com.squareup.retrofit2:converter-scalars:2.3.0'
        //ConverterFactory的Gson依赖包
        implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
        //CallAdapterFactory的Rx依赖包
        implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
        implementation 'com.jakewharton:butterknife:8.4.0'
        annotationProcessor  'com.jakewharton:butterknife-compiler:8.4.0'
        implementation 'com.android.support:recyclerview-v7:28.0.0-alpha1'
        implementation 'com.android.support:design:28.0.0-alpha1'
    

    greendao butterknife的引入还需再项目的gradle加入

     classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
     classpath 'org.greenrobot:greendao-gradle-plugin:3.2.1'
    
    • 1 设置沉浸式状态栏

     //沉浸式
        implementation 'com.jaeger.statusbarutil:library:1.5.1'
    

    在baseActivity中

     StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary));
    
    • 2 创建一个activity 管理类 AppActivityManager 管理activity 在oncreate 加入 onDestroy移出

    public class AppActivityManager {
        private static Stack<Activity> mActivityStack;
        private static AppActivityManager mAppManager;
    
        private AppActivityManager() {
        }
    
        /**
         * 单一实例
         */
        public static AppActivityManager getInstance() {
            if (mAppManager == null) {
                mAppManager = new AppActivityManager();
            }
            return mAppManager;
        }
    
        /**
         * 添加Activity到堆栈
         */
        public void addActivity(Activity activity) {
            if (mActivityStack == null) {
                mActivityStack = new Stack<Activity>();
            }
            mActivityStack.add(activity);
        }
        /**
         * 移除Activity到堆外
         */
        public void removeActivity(Activity activity) {
            mActivityStack.remove(activity);
        }
    
        /**
         * 获取栈顶Activity
         */
        public Activity getTopActivity() {
            return mActivityStack.lastElement();
        }
    
        /**
         * 结束栈顶Activity
         */
        public void killTopActivity() {
            Activity activity = mActivityStack.lastElement();
            killActivity(activity);
        }
    
        /**
         * 结束指定的Activity
         */
        private void killActivity(Activity activity) {
            if (activity != null) {
                mActivityStack.remove(activity);
                activity.finish();
            }
        }
    
        /**
         * 结束指定类名的Activity
         */
        public void killActivity(Class<?> cls) {
            for (Activity activity : mActivityStack) {
                if (activity.getClass().equals(cls)) {
                    killActivity(activity);
                }
            }
        }
    
        /**
         * 结束所有Activity
         */
        private void killAllActivity() {
            for (int i = 0, size = mActivityStack.size(); i < size; i++) {
                if (null != mActivityStack.get(i)) {
                    mActivityStack.get(i).finish();
                }
            }
            mActivityStack.clear();
        }
    
        /**
         * 退出应用程序
         */
        @SuppressWarnings("deprecation")
        public void AppExit(Context context) {
            try {
                killAllActivity();
                android.app.ActivityManager activityMgr = (android.app.ActivityManager) context
                        .getSystemService(Context.ACTIVITY_SERVICE);
                activityMgr.restartPackage(context.getPackageName());
                System.exit(0);
            } catch (Exception e) {
                Log.e("AppActivityManager",""+e);
            }
        }
    }
    
    • 3打开activity 的动画 在res 目录下创建anim 文件

    anim_slide_in_left.xml
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="-100%p" android:toXDelta="0%"
                   android:duration="@android:integer/config_mediumAnimTime"/>
        <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
               android:duration="@android:integer/config_mediumAnimTime" />
    </set>
    
    anim_slide_in_right.xml
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="100%p" android:toXDelta="0"
                   android:duration="@android:integer/config_mediumAnimTime"/>
        <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
               android:duration="@android:integer/config_mediumAnimTime" />
    </set>
    
    
    anim_slide_out_right.xml
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="0%p" android:toXDelta="100%"
                   android:duration="@android:integer/config_mediumAnimTime"/>
        <alpha android:fromAlpha="1.0" android:toAlpha="0"
               android:duration="@android:integer/config_mediumAnimTime" />
    </set>
    
    
    anim_slide_out_left.xm
    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate android:fromXDelta="0%p" android:toXDelta="-100%"
                   android:duration="@android:integer/config_mediumAnimTime"/>
        <alpha android:fromAlpha="1.0" android:toAlpha="0"
               android:duration="@android:integer/config_mediumAnimTime" />
    </set>
    
    
    
      @Override
        public void startActivity(Intent intent) {
            super.startActivity(intent);
            overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
        }
    
        @Override
        public void startActivityForResult(Intent intent, int requestCode) {
            super.startActivityForResult(intent, requestCode);
            overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
        }
    
        protected void openActivity(Class clazz){
            Intent intent = new Intent(this,clazz);
            startActivity(intent);
        }
    
        @Override
        public void finish() {
            super.finish();
    
            overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
        }
    
    
    • 4完整BaseActivity

    
    
    public abstract class BaseActivity  extends AppCompatActivity{
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(getLayoutId());
            ButterKnife.bind(this) ;
            setStatusBar();
            initView();
            initData();
            AppActivityManager.getInstance().addActivity(this);
        }
    
        private void setStatusBar() {
            StatusBarUtil.setColor(this, getResources().getColor(R.color.colorPrimary));
        }
    
    
        /**
         * 初始化view
         */
        protected abstract void initView();
    
    
        /**
         * 布局ID
         * @return
         */
        protected  abstract int getLayoutId();
    
        /**
         * 初始化数据
         */
        protected void initData(){}
    
    
    
    
        @Override
        public void startActivity(Intent intent) {
            super.startActivity(intent);
            overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
        }
    
        @Override
        public void startActivityForResult(Intent intent, int requestCode) {
            super.startActivityForResult(intent, requestCode);
            overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
        }
    
        protected void openActivity(Class clazz){
            Intent intent = new Intent(this,clazz);
            startActivity(intent);
        }
    
        @Override
        public void finish() {
            super.finish();
    
            overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
        }
    
    
        @Override
        protected void onDestroy() {
            AppActivityManager.getInstance().removeActivity(this);
            super.onDestroy();
        }
    
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    this.finish();
                    return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Android从零搭建项目01 -BaseActivity

          本文链接:https://www.haomeiwen.com/subject/fkrooqtx.html