1.依赖库
compile 'com.android.support:design:25.3.0'
compile 'com.android.support:recyclerview-v7:25.3.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:cardview-v7:25.3.0'
//volley
compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'com.jakewharton:butterknife:5.1.1'
compile 'org.greenrobot:eventbus:3.0.0'
2.BaseActivity
package com.huading.myvolley.base;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.WindowManager;
import com.huading.myvolley.R;
import com.huading.myvolley.utils.AppManager;
import com.huading.myvolley.widget.LoadingDialog;
import com.huading.myvolley.widget.StatusBarCompat;
import butterknife.ButterKnife;
/**
* 作者:LHZ on 2017/3/31 17:38
* 界面:
*/
public abstract class BaseActivity extends AppCompatActivity {
/**
* context
*/
protected Context mContext = null;
/**
* Screen information
*/
protected int mScreenWidth = 0;
protected int mScreenHeight = 0;
protected float mScreenDensity = 0.0f;
protected Toolbar mToolbar = null;
// 加载进度条
public LoadingDialog mLoadingDialog = null;
// 开启沉浸式状态栏
protected boolean statusBarCompat = true;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 获取上下文
mContext = this;
// 取得手机屏幕大小
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
mScreenDensity = displayMetrics.density;
mScreenHeight = displayMetrics.heightPixels;
mScreenWidth = displayMetrics.widthPixels;
AppManager.instance.addActivity(this);
// 获取资源文件
if (getContentViewLayoutID() != 0) {
setContentView(getContentViewLayoutID());
} else {
throw new IllegalArgumentException("You must return a right contentView layout resource Id");
}
// android 沉浸状态栏、透明状态栏
if (statusBarCompat) {
StatusBarCompat.compat(this, ContextCompat.getColor(this, R.color.colorPrimary));
transparent19and20();
}
initView();
initData();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
public void finish() {
super.finish();
// 注解注销
ButterKnife.reset(this);
AppManager.instance.removeActivity(this);
// activity进入方式
// overridePendingTransition(R.anim.right_in, R.anim.right_out);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
// 注解注入
ButterKnife.inject(this);
// 标题
mToolbar = ButterKnife.findById(this, R.id.common_toolbar);
if (mToolbar != null) {
setSupportActionBar(mToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
// 初始化标题
public void initToolbar(Toolbar toolbar) {
mToolbar = toolbar;
if (mToolbar != null) {
setSupportActionBar(mToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
// 使用handler
protected Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
onHandlerThread(msg);
}
};
protected void onHandlerThread(Message msg) {
}
/**
* bind layout resource file
*
* @return id of layout resource
*/
protected abstract int getContentViewLayoutID();
/**
* init all views
*/
protected abstract void initView();
/**
* init all events
*/
protected abstract void initData();
/**
* 获取toolBar
*
* @return toolBar
*/
// 从fragment中获取toolbar
public Toolbar getToolbar() {
return mToolbar;
}
// 全透明状态栏
protected void transparent19and20() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明底部导航栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
}
/**
* startActivity
*
* @param clazz
*/
protected void readyGo(Class<?> clazz) {
Intent intent = new Intent(this, clazz);
startActivity(intent);
}
/**
* startActivity with bundle
*
* @param clazz
* @param bundle
*/
protected void readyGo(Class<?> clazz, Bundle bundle) {
Intent intent = new Intent(this, clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivity(intent);
}
/**
* startActivityForResult
*
* @param clazz
* @param requestCode
*/
protected void readyGoForResult(Class<?> clazz, int requestCode) {
Intent intent = new Intent(this, clazz);
startActivityForResult(intent, requestCode);
}
/**
* startActivityForResult with bundle
*
* @param clazz
* @param requestCode
* @param bundle
*/
protected void readyGoForResult(Class<?> clazz, int requestCode, Bundle bundle) {
Intent intent = new Intent(this, clazz);
if (null != bundle) {
intent.putExtras(bundle);
}
startActivityForResult(intent, requestCode);
}
/**
* 显示刷新Loadding
*/
public void showLoadingDialog(String message) {
try {
mLoadingDialog = LoadingDialog.createDialog(this);
mLoadingDialog.setTitle(null);
mLoadingDialog.setCancelable(false);
mLoadingDialog.setMessage(message);
mLoadingDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
hideLoadingDialog();
}
return true;
}
});
if (!isFinishing()) {
mLoadingDialog.show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 隐藏刷新Loadding
*/
public void hideLoadingDialog() {
try {
if (mLoadingDialog != null) {
if (mLoadingDialog.animation != null) {
mLoadingDialog.animation.reset();
}
mLoadingDialog.dismiss();
mLoadingDialog = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}