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();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值