Android開發(fā)記錄主要記錄一些實(shí)用的開發(fā)模式,自定義控件視圖和一些第三方的引用。也省去記不住的繁瑣..
好了!本篇主要為了快速搭建一個(gè)MVP案例,實(shí)用為主?。≈劣贛VP模式介紹、MVC模式介紹這里就不啰嗦了,有好多大神詳細(xì)的分析,這里只放個(gè)MVP模式工作圖啦。

mvp.png
MVP模式的核心思想:
MVP把Activity中的UI邏輯抽象成View接口,把業(yè)務(wù)邏輯抽象成Presenter接口,Model類還是原來的Model。
接下來
我們以最簡單的Splash頁跳轉(zhuǎn)到MainActivity為一個(gè)例子,以下是代碼目錄,個(gè)人喜好加借鑒,也可以根據(jù)model,view,presenter來做區(qū)分!

目錄.png
第一步
SplashViewInterface
public interface SplashViewInterface {
void jumpToMainActivity();
}
第二步
SplashPresenter ,做一個(gè)簡單的2秒跳轉(zhuǎn)
public class SplashPresenter implements SplashInteractor.OnFinishedListener {
private SplashViewInterface splashViewInterface;
private SplashInteractor splashInteractor;
SplashPresenter(SplashViewInterface splashViewInterface, SplashInteractor splashInteractor) {
this.splashViewInterface = splashViewInterface;
this.splashInteractor = splashInteractor;
}
void onCreate() {
//延遲兩秒跳轉(zhuǎn)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (splashViewInterface != null) {
splashViewInterface.jumpToMainActivity();
}
}
}, 2000);
}
void onDestroy() {
splashViewInterface = null;
}
@Override
public void onFinished(String callString) {
if (splashViewInterface != null) {
}
}
}
第三步
SplashInteractor
public class SplashInteractor {
private String TAG = "SplashInteractor";
interface OnFinishedListener {
void onFinished(String callString);
}
//可做些http請求,返回接口如上
}
第四步
SplashActivity
public class SplashActivity extends AppCompatActivity implements SplashViewInterface {
private final String TAG = this.getClass().getSimpleName();
private SplashPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
presenter = new SplashPresenter(this, new SplashInteractor());
presenter.onCreate();
}
@Override
public void jumpToMainActivity() {
//跳轉(zhuǎn)到主界面
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.onDestroy();
}
}
結(jié)尾
當(dāng)你體會到回頭查看Activity內(nèi)代碼過長過多,然后發(fā)現(xiàn)不用在Activiy里寫一堆亂七八糟的邏輯以后,你就會發(fā)現(xiàn)它的好處了!