feat: 项目初始化
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package com.example.myapplication.activity;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.example.myapplication.base.BaseActivity;
|
||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||
import com.google.android.material.snackbar.Snackbar;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
import com.example.myapplication.R;
|
||||
|
||||
public class HomeActivity extends BaseActivity {
|
||||
@Override
|
||||
protected int getLayout() {
|
||||
return R.layout.activity_home;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initView() {
|
||||
super.initView();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initData() {
|
||||
super.initData();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.example.myapplication.activity;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.example.myapplication.R;
|
||||
import com.example.myapplication.base.BaseActivity;
|
||||
|
||||
public class MainActivity extends BaseActivity {
|
||||
|
||||
@Override
|
||||
protected int getLayout() {
|
||||
return R.layout.activity_main;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initData() {
|
||||
super.initData();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initView() {
|
||||
super.initView();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.example.myapplication.api;
|
||||
|
||||
public interface ApiService {
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.myapplication.api;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
|
||||
public class UserApplication extends Application {
|
||||
public static UserApplication userApplication;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
userApplication=this;
|
||||
}
|
||||
|
||||
public static UserApplication getUserApplication() {
|
||||
return userApplication;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.example.myapplication.base;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import android.widget.Toast;
|
||||
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.example.myapplication.api.UserApplication;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.Unbinder;
|
||||
|
||||
public abstract class BaseActivity extends AppCompatActivity {
|
||||
|
||||
private Unbinder bind;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(getLayout());
|
||||
bind = ButterKnife.bind(this);
|
||||
initMVP();
|
||||
initView();
|
||||
initData();
|
||||
initListener();
|
||||
}
|
||||
protected void toast(String mag){
|
||||
Toast.makeText(UserApplication.getUserApplication(), mag, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
protected void initMVP() {
|
||||
}
|
||||
|
||||
protected void initListener() {
|
||||
|
||||
}
|
||||
|
||||
protected abstract int getLayout();
|
||||
|
||||
protected void initView() {
|
||||
|
||||
}
|
||||
|
||||
protected void initData() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
bind.unbind();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.example.myapplication.base;
|
||||
|
||||
public interface BaseCallBack<T> {
|
||||
void onSuccess(T t);
|
||||
void onFail(String error);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.example.myapplication.base;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import butterknife.ButterKnife;
|
||||
import butterknife.Unbinder;
|
||||
|
||||
public abstract class BaseFragment extends Fragment {
|
||||
|
||||
private Unbinder bind;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
View inflate = inflater.inflate(getLayout(), container, false);
|
||||
bind = ButterKnife.bind(this, inflate);
|
||||
initMvp();
|
||||
initView();
|
||||
initData();
|
||||
return inflate;
|
||||
}
|
||||
|
||||
protected void initMvp() {
|
||||
|
||||
}
|
||||
|
||||
protected abstract int getLayout();
|
||||
|
||||
protected void initData() {
|
||||
|
||||
}
|
||||
|
||||
protected void initView() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
bind.unbind();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.example.myapplication.base;
|
||||
|
||||
public abstract class BaseMVPActivity <P extends BasePresenter,V extends BaseView> extends BaseActivity{
|
||||
protected P presenter;
|
||||
|
||||
@Override
|
||||
protected void initMVP() {
|
||||
super.initMVP();
|
||||
presenter= initMVPPresenter();
|
||||
if (presenter!=null){
|
||||
presenter.setView(initMVPView());
|
||||
} }
|
||||
|
||||
protected abstract V initMVPView();
|
||||
|
||||
protected abstract P initMVPPresenter();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.example.myapplication.base;
|
||||
|
||||
public abstract class BaseMVPFragment<P extends BasePresenter,V extends BaseView> extends BaseFragment{
|
||||
protected P presenter;
|
||||
|
||||
@Override
|
||||
protected void initMvp() {
|
||||
super.initMvp();
|
||||
presenter= initMVPPresenter();
|
||||
if (presenter!=null){
|
||||
presenter.setView(initMVPView());
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract V initMVPView();
|
||||
|
||||
protected abstract P initMVPPresenter();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.myapplication.base;
|
||||
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
|
||||
public abstract class BaseModel {
|
||||
private CompositeDisposable compositeDisposable;
|
||||
public void onDestroy(){
|
||||
if (compositeDisposable!=null){
|
||||
compositeDisposable.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.example.myapplication.base;
|
||||
|
||||
import android.net.ParseException;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.example.myapplication.api.UserApplication;
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.io.InterruptedIOException;
|
||||
import java.net.ConnectException;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import io.reactivex.subscribers.ResourceSubscriber;
|
||||
import retrofit2.HttpException;
|
||||
|
||||
public abstract class BaseObserver<T> extends ResourceSubscriber <T> {
|
||||
@Override
|
||||
public void onNext(T t) {
|
||||
onSuccess(t);
|
||||
}
|
||||
public abstract void onSuccess(T t);
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
Log.e("TAG", "error:" + e.getMessage());
|
||||
//对异常进行分类,不同的异常提示用户不同的信息
|
||||
if (e instanceof HttpException) {
|
||||
// HTTP错误
|
||||
onFail("HTTP错误");
|
||||
} else if (e instanceof ConnectException
|
||||
|| e instanceof UnknownHostException) {
|
||||
// 连接错误
|
||||
onFail("连接错误");
|
||||
} else if (e instanceof InterruptedIOException) {
|
||||
// 连接超时
|
||||
onFail("连接超时");
|
||||
} else if (e instanceof JsonParseException
|
||||
|| e instanceof JSONException
|
||||
|| e instanceof ParseException) {
|
||||
// 解析错误
|
||||
onFail("解析错误");
|
||||
} else {
|
||||
if (e != null) {
|
||||
onFail(e.toString());
|
||||
} else {
|
||||
onFail("未知错误");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void onFail(String error){
|
||||
Toast.makeText(UserApplication.getUserApplication(), error, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.example.myapplication.base;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public abstract class BasePresenter<V extends BaseView> {
|
||||
protected V view;
|
||||
private ArrayList<BaseModel> list=new ArrayList<>();
|
||||
public void setView(V view) {
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
public BasePresenter() {
|
||||
initModel();
|
||||
}
|
||||
public void onDestroy(){
|
||||
view=null;
|
||||
if (list.size()>0){
|
||||
for (BaseModel baseModel : list) {
|
||||
baseModel.onDestroy();
|
||||
}
|
||||
list.clear();
|
||||
}
|
||||
}
|
||||
public void addModel(BaseModel baseModel){
|
||||
list.add(baseModel);
|
||||
}
|
||||
protected abstract void initModel();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.example.myapplication.base;
|
||||
|
||||
public interface BaseView<T> {
|
||||
void onSuccess(T t);
|
||||
void onFail(String error);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.myapplication.fragment;
|
||||
|
||||
import com.example.myapplication.R;
|
||||
import com.example.myapplication.base.BaseFragment;
|
||||
|
||||
/**
|
||||
* 发现的Fragment
|
||||
* 2021-5-25
|
||||
*/
|
||||
public class FindFragment extends BaseFragment {
|
||||
@Override
|
||||
protected int getLayout() {
|
||||
return R.layout.find_fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initView() {
|
||||
super.initView();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initData() {
|
||||
super.initData();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.myapplication.fragment;
|
||||
|
||||
import com.example.myapplication.R;
|
||||
import com.example.myapplication.base.BaseFragment;
|
||||
|
||||
/**
|
||||
* 我的的Fragment
|
||||
* 2021-5-25
|
||||
*/
|
||||
public class MineFragment extends BaseFragment {
|
||||
@Override
|
||||
protected int getLayout() {
|
||||
return R.layout.mine_fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initView() {
|
||||
super.initView();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initData() {
|
||||
super.initData();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.myapplication.fragment;
|
||||
|
||||
import com.example.myapplication.R;
|
||||
import com.example.myapplication.base.BaseFragment;
|
||||
|
||||
/**
|
||||
* 记录的Fragment
|
||||
* 2021-5-25
|
||||
*/
|
||||
public class RecordFragment extends BaseFragment {
|
||||
@Override
|
||||
protected int getLayout() {
|
||||
return R.layout.record_fragment;
|
||||
}
|
||||
@Override
|
||||
protected void initView() {
|
||||
super.initView();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initData() {
|
||||
super.initData();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.myapplication.fragment;
|
||||
|
||||
import com.example.myapplication.R;
|
||||
import com.example.myapplication.base.BaseFragment;
|
||||
|
||||
/**
|
||||
* 寻宝的Fragment
|
||||
* 2021-5-25
|
||||
*/
|
||||
public class TreasureFragment extends BaseFragment {
|
||||
@Override
|
||||
protected int getLayout() {
|
||||
return R.layout.treasure_fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initView() {
|
||||
super.initView();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initData() {
|
||||
super.initData();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.example.myapplication.net;
|
||||
|
||||
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.OkHttpClient;
|
||||
import retrofit2.Converter;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class DataService {
|
||||
|
||||
private static final long DEFAULT_TIMEOUT = 20000; // 默认超时20s
|
||||
private static Object mApiService;
|
||||
|
||||
private static ArrayList<Interceptor> mInterceptors;
|
||||
private static Converter.Factory mFactor;
|
||||
private static Class mServiceClass;
|
||||
private static String mBaseUrl;
|
||||
|
||||
public static void init(Class aClass, String baseUrl, ArrayList<Interceptor> interceptor, Converter.Factory factor) {
|
||||
mServiceClass = aClass;
|
||||
mBaseUrl = baseUrl;
|
||||
mInterceptors = interceptor;
|
||||
mFactor = factor;
|
||||
|
||||
}
|
||||
|
||||
public static void init(Class aClass, String baseUrl, ArrayList<Interceptor> interceptor) {
|
||||
init(aClass, baseUrl, interceptor, null);
|
||||
|
||||
}
|
||||
|
||||
public static void init(Class aClass, String baseUrl, Converter.Factory factor) {
|
||||
init(aClass, baseUrl, null, factor);
|
||||
|
||||
}
|
||||
|
||||
public static void init(Class aClass, String baseUrl) {
|
||||
init(aClass, baseUrl, null, null);
|
||||
|
||||
}
|
||||
|
||||
public static synchronized Object getApiService() {
|
||||
|
||||
if (mServiceClass == null) {
|
||||
throw new NullPointerException("在调用 DataService 的 getApiService 之前必须调用 init 方法惊喜初始化");
|
||||
}
|
||||
|
||||
if (mApiService == null) {
|
||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||
if (mInterceptors != null && mInterceptors.size() > 0) {
|
||||
for (Interceptor interceptor : mInterceptors) {
|
||||
builder.addInterceptor(interceptor);
|
||||
}
|
||||
}
|
||||
|
||||
builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
|
||||
.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
|
||||
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
|
||||
.build();
|
||||
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.client(builder.build())
|
||||
.addConverterFactory(mFactor == null ? GsonConverterFactory.create() : mFactor) // 帮我们把json 窜转为 entity 对象
|
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // 结合 rxjava 使用
|
||||
.baseUrl(mBaseUrl)
|
||||
.build();
|
||||
|
||||
mApiService = retrofit.create(mServiceClass);
|
||||
}
|
||||
|
||||
return mApiService;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.example.myapplication.util;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import android.util.AttributeSet;
|
||||
import android.view.MotionEvent;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.viewpager.widget.ViewPager;
|
||||
|
||||
//自定义不可滑动的VI
|
||||
public class NoSlideViewPager extends ViewPager {
|
||||
// 定义一个是否可以滑动的boolean 值
|
||||
private boolean isCanScroll = false;
|
||||
|
||||
public NoSlideViewPager(@NonNull Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
public NoSlideViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
|
||||
// 滑动到指定位置
|
||||
@Override
|
||||
public void scrollTo(int x, int y) {
|
||||
super.scrollTo(x, y);
|
||||
}
|
||||
|
||||
// 触摸事件
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent ev) {
|
||||
if (isCanScroll) {
|
||||
return super.onTouchEvent(ev);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 设置当前显示的布局
|
||||
@Override
|
||||
public void setCurrentItem(int item) {
|
||||
super.setCurrentItem(item);
|
||||
}
|
||||
|
||||
// 设置当前显示的布局,并定义滑动方式
|
||||
@Override
|
||||
public void setCurrentItem(int item, boolean smoothScroll) {
|
||||
super.setCurrentItem(item, smoothScroll);
|
||||
}
|
||||
|
||||
|
||||
// 拦截触摸事件
|
||||
@Override
|
||||
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
||||
if (isCanScroll) {
|
||||
return super.onInterceptTouchEvent(ev);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user