butterknife需要的依赖
implementation 'com.jakewharton:butterknife:10.2.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
implementation 'com.github.tongchexinfeitao:QRCodeLibrary:1.0'
defaultConfig里
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
app的build.gradle最上面
apply plugin: 'com.jakewharton.butterknife'
根目录repositories里
mavenCentral()
maven { url 'https://jitpack.io' }
dependencies里
classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.1'
retrofit需要的依赖
//retrofit核心库
implementation 'com.squareup.retrofit2:retrofit:2.6.2'。
//retrofit辅助,解析gson的库
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.2'
//okhttp日志拦截器
implementation 'com.squareup.okhttp3:logging-interceptor:4.2.2'
抽基类封装BasePresenter
public abstract class BasePresenter<M extends BaseModel,V extends BaseView>{
public M model;
public WeakReference<V> weakReference;
public BasePresenter(){
model=initModel();
}
public void attch(V v){
weakReference=new WeakReference<>(v);
}
public void deatch(){
if (weakReference != null) {
weakReference.clear();
weakReference=null;
}
}
protected abstract M initModel();
public V getView(){
return weakReference.get();
}
}
基类封装BaseActivity
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView{
public P presenter;
public Unbinder unbinder;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(LayoutId());
unbinder= ButterKnife.bind(this);
presenter=initPresenter();
if (presenter != null) {
presenter.attch(this);
}
initView();
initData();
}
protected abstract P initPresenter();
protected abstract void initData();
protected abstract void initView();
protected abstract int LayoutId();
@Override
protected void onDestroy() {
super.onDestroy();
if (presenter != null) {
presenter.deatch();
}
if (unbinder != null) {
unbinder.unbind();
}
}
}
基类封装BaseFragment
public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView {
public P presenter;
public Unbinder unbinder;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View inflate = View.inflate(getActivity(), LayoutId(), null);
unbinder= ButterKnife.bind(this,inflate);
presenter=initPresenter();
if (presenter != null) {
presenter.attch(this);
}
initView(inflate);
return inflate;
}
protected abstract void initView(View inflate);
protected abstract P initPresenter();
protected abstract int LayoutId();
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
}
protected abstract void initData();
@Override
public void onDestroy() {
super.onDestroy();
if (presenter != null) {
presenter.deatch();
}
if (unbinder != null) {
unbinder.unbind();
}
}
}
封装RetrofitUtils工具类
public class RetrofitUtils {
private static RetrofitUtils retrofitUtils;
private final Retrofit retrofit;
private RetrofitUtils(){
OkHttpClient okHttpClient=new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(5,TimeUnit.SECONDS)
.writeTimeout(5,TimeUnit.SECONDS)
.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
retrofit = new Retrofit.Builder()
.baseUrl(Api.BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
public static RetrofitUtils getInstance() {
if (retrofitUtils==null){
synchronized (RetrofitUtils.class){
if (retrofitUtils==null){
retrofitUtils=new RetrofitUtils();
}
}
}
return retrofitUtils;
}
public<T> T createService(Class<T> tClass){
T t=retrofit.create(tClass);
return t;
}
}