BaseActivity:ButterKnife初始化,沉浸式状态栏,Eventbus初始化,统一字体,统一跳转动画
依赖包:
implementation 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' implementation 'org.greenrobot:eventbus:3.0.0' implementation 'com.android.support:multidex:1.0.3' //okhttp implementation 'com.squareup.okhttp3:okhttp:3.10.0' //retrofit implementation 'com.squareup.retrofit2:retrofit:2.4.0' implementation 'com.squareup.retrofit2:converter-gson:2.3.0' implementation 'com.squareup.retrofit2:adapter-rxjava:2.3.0' //rxjava implementation 'io.reactivex:rxandroid:1.2.1' implementation 'io.reactivex.rxjava2:rxjava:2.1.12' // alternatively, just ViewModel implementation "android.arch.lifecycle:viewmodel:1.1.1" // alternatively, just LiveData implementation "android.arch.lifecycle:livedata:1.1.1" annotationProcessor "android.arch.lifecycle:compiler:1.1.1" //java8使用这个依赖 // implementation "android.arch.lifecycle:common-java8:1.1.1" //下拉刷新自定义头部view implementation 'com.yanzhenjie:recyclerview-swipe:1.1.4'
import android.annotation.TargetApi; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import butterknife.Unbinder; public abstract class BaseActivity extends AppCompatActivity { private static final String TAG = BaseActivity.class.getSimpleName(); protected Unbinder mUnbinder; protected abstract void initView(); protected abstract void initData(); BaseActivity self; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); self = this; steepStatusBar(); App.getApp().addActivity(this); initView(); initData(); } /** * 内存不够时 * * @param level */ @Override public void onTrimMemory(int level) { super.onTrimMemory(level); if (level == TRIM_MEMORY_MODERATE) { App.getApp().exit(); } } @Override protected void onDestroy() { super.onDestroy(); App.getApp().removeActivity(this); } /** * [沉浸状态栏] */ private void steepStatusBar() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { setTranslucentStatus(); } switch (RomUtil.getLightStatausBarAvailableRomType()) { case RomUtil.AvailableRomType.MIUI: LightStatusBarUtil.setLightStatusBar(self, true); break; case RomUtil.AvailableRomType.FLYME: LightStatusBarUtil.setLightStatusBar(self, true); break; case RomUtil.AvailableRomType.ANDROID_NATIVE: LightStatusBarUtil.setLightStatusBar(self, true); break; case RomUtil.AvailableRomType.NA: LightStatusBarUtil.setLightStatusBar(self, false); break; } } @TargetApi(19) protected void setTranslucentStatus() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Window window = getWindow(); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.setStatusBarColor(getResources().getColor(R.color.colorPrimary)); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { Window window = getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } } /** * 设置粗体样式 * * @param textViews */ protected void setBoldTypeFace(TextView... textViews) { FontUtils.setBoldTypeface(textViews); } /** * 设置Light样式 * * @param textViews */ protected void setLightTypeface(TextView... textViews) { FontUtils.setLightTypeface(textViews); } protected void registerEventBus(Object object) { if (!EventBus.getDefault().isRegistered(object)) { EventBus.getDefault().register(object); } } protected void unregisterEventBus(Object object) { if (EventBus.getDefault().isRegistered(object)) { EventBus.getDefault().unregister(object); } } @Override public void startActivity(Intent intent) { super.startActivity(intent); overridePendingTransition(R.anim.activity_in,R.anim.activity_out); } }
附上两个动画
BaseFragment:ButterKnife初始化,Eventbus初始化,统一字体,
import android.content.Intent; import android.support.v4.app.Fragment; import org.greenrobot.eventbus.EventBus; import butterknife.Unbinder; public abstract class BaseFragment extends Fragment { protected Unbinder mUnbinder; protected abstract void initView(); protected abstract void initData(); protected void lossAuthority() { } protected void registerEventBus(Object object) { if (!EventBus.getDefault().isRegistered(object)) { EventBus.getDefault().register(object); } } protected void unregisterEventBus(Object object) { if (EventBus.getDefault().isRegistered(object)) { EventBus.getDefault().unregister(object); } } }
BaseLazyFragment:懒加载fragment
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; /** * A simple {@link Fragment} subclass. */ public abstract class BaseLazyFragment extends Fragment { boolean isUiVisible; boolean isViewCreate; boolean isLoadedData; public BaseLazyFragment() { // Required empty public constructor } @Override public void onResume() { super.onResume(); } @Override public void onPause() { super.onPause(); } @Override public void onDestroy() { super.onDestroy(); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); isViewCreate = true; lazyLoad(); } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); isUiVisible = isVisibleToUser; lazyLoad(); } protected void lazyLoad(){ if (isUiVisible && isViewCreate && !isLoadedData){ loadData(); isLoadedData = true; } } public abstract void loadData(); }
BaseArrayBean,BaseObjectBean,MyObserver:在使用retrofit+okhttp时,基本会统一返回类型,而加上MyObserver时能够在返回某些特殊异常时一个方法统一处理,让Observer回调少些代码
import java.util.List; public class BaseArrayBean{ private String code; private String msg; private List data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public List getData() { return data; } public void setData(List data) { this.data = data; } @Override public String toString() { return "HttpResult{" + "code=" + code + ", msg=" + msg + ", data=" + data + '}'; } }
public class BaseObjectBean{ private String code; private String msg; private T data; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public T getData() { return data; } public void setData(T data) { this.data = data; } @Override public String toString() { return "HttpResult{" + "code=" + code + ", msg=" + msg + ", data=" + data + '}'; } }
import android.util.Log; import rx.Observer; public class MyObserverimplements Observer { @Override public void onCompleted() { } @Override public void onError(Throwable e) { e.printStackTrace(); } @Override public void onNext(T t) { if (t != null) { String code = "8001"; if (t instanceof BaseObjectBean) { code = ((BaseObjectBean) t).getCode(); } else if (t instanceof BaseArrayBean) { code = ((BaseArrayBean) t).getCode(); } } } }