MVP模式

来源:互联网 发布:拉圈圈助手源码 编辑:程序博客网 时间:2024/06/06 17:09

View: 对应于Activity,负责View的绘制以及与用户交互
Model :依然是业务逻辑和实体模型
Presenter: 负责完成View于Model间的交互

view 部分:
SplashView代码:

public interface SplashView {    void navigateToHomePage();    void animateBackgroundImage(Animation animation);    void initView(int resourceId);}

MainActivity代码:

package com.xl.test.view;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.animation.Animation;import android.widget.ImageView;import android.widget.Toast;import com.xl.test.R;import com.xl.test.presenter.Presenter;import com.xl.test.presenter.SplashPresenterImpl;import butterknife.BindView;import butterknife.ButterKnife;public class MainActivity extends AppCompatActivity implements SplashView {    @BindView(R.id.splash_image)    ImageView splashImage;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);        Presenter presenter = new SplashPresenterImpl(this, this);        presenter.initialized();    }    @Override    public void navigateToHomePage() {        Toast.makeText(this, "结束", 1).show();    }    @Override    public void animateBackgroundImage(Animation animation) {        splashImage.startAnimation(animation);    }    @Override    public void initView(int resourceId) {        splashImage.setImageResource(resourceId);    }}

model部分:

SplashInteractor代码:

package com.xl.test.model;import android.content.Context;import android.view.animation.Animation;/** * Created by hushendian on 2017/7/19. */public interface SplashInteractor {    int getBackgroundImageResID();    Animation getBackgroudImageAnimation(Context context);}

SplashInteraatorImpl代码:

package com.xl.test.model;import android.content.Context;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import com.xl.test.R;/** * Created by hushendian on 2017/7/19. */public class SplashInteractorImpl implements SplashInteractor {    @Override    public int getBackgroundImageResID() {        return R.mipmap.morning;    }    @Override    public Animation getBackgroudImageAnimation(Context context) {        return AnimationUtils.loadAnimation(context, R.anim.splash);    }}

presenter部分:
Presenter代码:

package com.xl.test.presenter;/** * Created by hushendian on 2017/7/19. */public interface Presenter {    void initialized();}

SplashPresenterImpl代码:

package com.xl.test.presenter;import android.content.Context;import android.view.animation.Animation;import com.xl.test.model.SplashInteractorImpl;import com.xl.test.view.SplashView;/** * Created by hushendian on 2017/7/19. */public class SplashPresenterImpl implements Presenter {    Context mContext;    SplashView mSplashView;    SplashInteractorImpl mSplashInteractor;    public SplashPresenterImpl(Context context, SplashView splashView) {        if (null == splashView) {            throw new IllegalArgumentException("Constructor's parameters must not be Null");        }        mContext = context;        mSplashView = splashView;        mSplashInteractor = new SplashInteractorImpl();    }    @Override    public void initialized() {        mSplashView.initView(mSplashInteractor.getBackgroundImageResID());        Animation animation = mSplashInteractor.getBackgroudImageAnimation(mContext);        animation.setAnimationListener(new Animation.AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {            }            @Override            public void onAnimationEnd(Animation animation) {                mSplashView.navigateToHomePage();            }            @Override            public void onAnimationRepeat(Animation animation) {            }        });        mSplashView.animateBackgroundImage(animation);    }}

github地址:https://github.com/hushendian/GitWork.git