MVP-1封装-使用弱引用和rxjava解决内存泄漏问题
一.Model层封装
1.RetrofitManager:懒汉双重锁
public class RetrofitManager {
//单例模式:懒汉双重锁校验 节省资源
private RetrofitManager(){}
private static RetrofitManager sRetrofitManager;//区分成员变量m 静态变量s 局部变量
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;
}
//构建Retrofit对象
private void createRetrofit() {
mRetrofit = new Retrofit.Builder()
.baseUrl("http://82.156.173.100:8087")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
}
2.ApiServer
public interface ApiServer {
//分页加载
@GET("video/findVideos?pageSize=200")
Observable<VideoEntity> video(@Query("currentPage") int currentPage);
}
3.BaseModel
//网络请求
public class BaseModel {
//网络请求接口文档 private:只能被本类访问 protected:被本类以及实现子类访问
protected ApiServer mApiServer;
public BaseModel() {
mApiServer = RetrofitManager.getInstance().getRetrofit().create(ApiServer.class);
}
}
二.Presenter层封装
//绑定解析View 以图和数据交互
public class BasePresenter<V> {
//用弱引用创建View
//弱引用 ->解决MVP的内存泄露->OOM内存溢出
//强 软 弱 虚
WeakReference<V> mWeakReference;
//请求方式 okHttp retrofit rxjava
//每个请求 都会产生订阅->把订阅做一个统一的管理
//将rxjava的 订阅 添加到mCompositeDisposable 通过mCompositeDisposable 对订阅的请求做统一的管理
protected CompositeDisposable mCompositeDisposable = new CompositeDisposable();
// 通过mWeakReference创建View
public void attachView(V view){
mWeakReference = new WeakReference<V>(view);
}
//取出对应的view
protected V getView(){
return mWeakReference == null?null:mWeakReference.get();
}
//在咱们取出对应的view之前做这个判断
protected boolean isViewAttached(){
return mWeakReference != null && mWeakReference.get() !=null;
}
//在APP或者页面的生命周期结束 及其他需要的地方 释放View 取消订阅
public void detachView(){
// 释放View
if (mWeakReference != null){
mWeakReference.clear();
mWeakReference = null;
}
// 取消订阅
mCompositeDisposable.clear();
}
}
三.View层封装
1.IBaseView
//展示 view最基础的能力
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;
//初始化数据:创建mPresenter
protected abstract void initData();
//初始化
protected abstract void initView(Bundle savedInstanceState);
//布局id
protected abstract int bindLayout();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(bindLayout());
initView(savedInstanceState);
initData();
//使用弱引用创建View
if(mPresenter != null){
mPresenter.attachView((V)this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//释放view 取消订阅
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();
}
}
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();
//使用弱引用创建View
if(mPresenter != null){
mPresenter.attachView((V)this);
}
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mPresenter != null){
//释放订阅 销毁view
mPresenter.detachView();
}
}
//多的方法:findViewById
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();
}
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgccbhe
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13