美文网首页MVP
Android 结构之MVP

Android 结构之MVP

作者: sunjiandev | 来源:发表于2019-11-07 20:34 被阅读0次

    Android 结构之MVP

    mvp 作为Android 开发中一种典型的架构模式,其优点不言而且,代码逻辑清晰,易扩展,解耦性强,如果封装的号,后期版本迭代,界面ui变化,会很方便。但是,如果写的不好,则会又内存泄漏的风险!本分的目的就是封装一个没有内存泄漏的mvp架构模式

    BasePresenter

    添加弱引用的支持,防止内存泄漏

    import java.lang.ref.WeakReference;
    
    /**
     * Copyright (C), 2015-2019, 北京xxxxxx有限公司
     * Author:
     * Date: 2019/8/27 20:15
     * Description:
     */
    public class BasePresenter<T> {
    
        private WeakReference<T> mViewRef;
        protected BaseActivity activity;
    
        public void attachView(T view) {
            mViewRef = new WeakReference<>(view);
        }
    
        public BasePresenter(BaseActivity activity) {
            this.activity = activity;
        }
    
        protected T getView() {
            if (mViewRef != null) {
                return mViewRef.get();
            } else {
                return null;
            }
        }
    
        public boolean isViewAttach() {
            return getView() != null;
        }
    
        protected void detachView() {
            if (mViewRef != null) {
                mViewRef.clear();
                mViewRef = null;
            }
        }
    }
    
    
    

    BaseActivity

    BaseActivity 的封装

    
    import android.os.Bundle;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    
    /**
     * Copyright (C), 2015-2019, 北京xxxxxx有限公司
     * Author:
     * Date: 2019/8/27 20:20
     * Description:
     */
    public abstract class BaseActivity<V, T extends BasePresenter<V>> extends AppCompatActivity {
    
        protected T presenter;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(initLayout());
    
            presenter = createPresenter();
            initView();
    
            if (presenter!=null){
                presenter.attachView((V) this);
            }
        }
    
        protected abstract void initView();
    
        protected abstract T createPresenter();
    
        protected abstract int initLayout();
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            presenter.detachView();
        }
    }
    
    

    ok,封装基本完成,开始使用,以登录为例,先看项目结构

    /**
     * Copyright (C), 2015-2019, 北京xxxxx有限公司
     * Author:
     * Date: 2019/8/27 20:25
     * Description:
     */
    public interface LoginContract {
        interface Model {
        }
    
        interface View {
            void onSuccess();
    
            void onFailed(String reason);
        }
    
        interface Presenter {
            void login(String userName, String passWord);
        }
    }
    
    
    ...
    
    
    import com.sunkaisens.androidutils.base.BaseActivity;
    import com.sunkaisens.androidutils.base.BasePresenter;
    import com.sunkaisens.app.contract.LoginContract;
    
    /**
     * Copyright (C), 2015-2019, 北京xxxxxxx有限公司
     * Author:
     * Date: 2019/8/27 20:25
     * Description:
     */
    public class LoginPresenter extends BasePresenter<LoginContract.View>implements LoginContract.Presenter {
    
    
        public LoginPresenter(BaseActivity activity) {
            super(activity);
        }
    
        @Override
        public void login(String userName, String passWord) {
    
            if ("sunjianyun".equals(userName)&&"123456".equals(passWord)){
                getView().onSuccess();
            }else {
                getView().onFailed("用户名或密码错误");
            }
        }
    }
    
    
    ...
    
    /**
     * Copyright (C), 2015-2019, 北京视游互动科技有限公司
     * Author: sunjianyun
     * Date: 2019/8/27 20:24
     * Description:
     */
    public class LoginActivity extends BaseActivity<LoginContract.View, LoginPresenter> implements LoginContract.View {
        @Override
        protected void initView() {
    
        }
    
        @Override
        protected LoginPresenter createPresenter() {
            return new LoginPresenter(this);
        }
    
        @Override
        protected int initLayout() {
            return R.layout.activity_login;
        }
    
        @Override
        public void onSuccess() {
    
            ToastUtil.showToast("登录成功");
        }
    
        @Override
        public void onFailed(String reason) {
            ToastUtil.showToast("登录失败:"+reason);
    
        }
    
        public void login(View view) {
            presenter.login("sunjianyun","123456");
        }
    }
    
    
    

    相关文章

      网友评论

        本文标题:Android 结构之MVP

        本文链接:https://www.haomeiwen.com/subject/vfqbbctx.html