一.Model层封装
1.RetrofitManager:懒汉双重锁
public class RetrofitManager {
private RetrofitManager(){}
private static RetrofitManager sRetrofitManager;
public static RetrofitManager getInstance(){
if(sRetrofitManager == null){
synchronized (RetrofitManager.class){
if(sRetrofitManager == null){
sRetrofitManager = new RetrofitManager();
}
}
}
return sRetrofitManager;
}
private Retrofit mRetrofit;
public Retrofit getRetrofit() {
if(mRetrofit == null){
createRetrofit();
}
return mRetrofit;
}
private void createRetrofit() {
mRetrofit = new Retrofit.Builder()
.baseUrl("http://82.156.173.100:8087")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
2.ApiServer
public interface ApiServer {
@GET("video/findVideos?pageSize=200")
Observable<VideoEntity> video(@Query("currentPage") int currentPage);
}
3.BaseModel
public class BaseModel {
protected ApiServer mApiServer;
public BaseModel() {
mApiServer = RetrofitManager.getInstance().getRetrofit().create(ApiServer.class);
}
}
二.Presenter层封装
public class BasePresenter<V> {
WeakReference<V> mWeakReference;
protected CompositeDisposable mCompositeDisposable = new CompositeDisposable();
public void attachView(V view){
mWeakReference = new WeakReference<V>(view);
}
protected V getView(){
return mWeakReference == null?null:mWeakReference.get();
}
protected boolean isViewAttached(){
return mWeakReference != null && mWeakReference.get() !=null;
}
public void detachView(){
if (mWeakReference != null){
mWeakReference.clear();
mWeakReference = null;
}
mCompositeDisposable.clear();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
三.View层封装
1.IBaseView
public interface IBaseView {
void onLoading();
void onLoadSuccess();
void onLoadFailed();
}
2.BaseActivity
public abstract class BaseActivity<V,P extends BasePresenter<V>> extends AppCompatActivity implements IBaseView{
protected P mPresenter;
protected abstract void initData();
protected abstract void initView(Bundle savedInstanceState);
protected abstract int bindLayout();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(bindLayout());
initView(savedInstanceState);
initData();
if(mPresenter != null){
mPresenter.attachView((V)this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mPresenter != null){
mPresenter.detachView();
}
}
@Override
public void onLoading() {
Toast.makeText(this, "数据加载中", Toast.LENGTH_SHORT).show();
}
@Override
public void onLoadSuccess() {
Toast.makeText(this, "数据加载成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onLoadFailed() {
Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
3.BaseFragment
public abstract class BaseFragment<V,P extends BasePresenter<V>> extends Fragment implements IBaseView {
protected P mPresenter;
protected abstract void initData();
protected abstract void initView();
protected abstract int bindLayout();
protected View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(bindLayout(),container,false);
initView();
initData();
if(mPresenter != null){
mPresenter.attachView((V)this);
}
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mPresenter != null){
mPresenter.detachView();
}
}
public <T extends View> T findViewById(@IdRes int id) {
return mView.findViewById(id);
}
@Override
public void onLoading() {
Toast.makeText(getContext(), "数据加载中", Toast.LENGTH_SHORT).show();
}
@Override
public void onLoadSuccess() {
Toast.makeText(getContext(), "数据加载成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onLoadFailed() {
Toast.makeText(getContext(), "数据加载失败", Toast.LENGTH_SHORT).show();
}
}

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48