此外 ListView 的源码实现中还是用了观察者设计模式 其中的玄机就在这个方法中
mAdapter.notifyDataSetChanged();
MODEL:
public interface Iconnectivity {
boolean isOnline();
}
public class Connectivity implements Iconnectivity {
@Override
public boolean isOnline() {
return readFromNetWork();
}
private boolean readFromNetWork(){
//... 经过一些业务逻辑后
return false; // 返回相应结果
}
}
VIEW:
public class SplashActivity extends AppCompatActivity implements ISplash {
private SplashPresenter mSp;
private TextView textView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//...初始化
textView = ((TextView) findViewById(R.id.textView));
mSp = new SplashPresenter(this);
mSp.checkConnectivity();
}
@Override
public void hiddenProgressBar() {
textView.setVisibility(View.GONE);
}
@Override
public void showProgressBar() {
textView.setVisibility(View.VISIBLE);
}
@Override
public void setError() {
}
@Override
public void jumpToMainActivity() {
}
}
public interface ISplash {
void hiddenProgressBar();
void showProgressBar();
void setError();
void jumpToMainActivity();
}
PRESENTER:
public class SplashPresenter {
Iconnectivity mConn;
ISplash mSplash;
public SplashPresenter(ISplash s) {
this.mSplash = s;
mConn = new Connectivity();
}
public void checkConnectivity() {
if (mConn.isOnline()) {
mSplash.hiddenProgressBar();
mSplash.jumpToMainActivity();
} else {
mSplash.showProgressBar();
mSplash.setError();
}
}
}