MVP框架搭建
MVP框架分为三层M层(获取数据层),P层(中间层),V层(视图层)
先从M层开始搭建
IModel接口内容
public interface IModel {
void destory();
}
BaseModel
public class BaseModel implements IModel {
@Override
public void destory() {
}
}
P层(中间层)
IPresenter
public interface IPresenter {
void destory();
}
BasePresenter
public class BasePresenter<M extends IModel,V extends IView> implements IPresenter {
protected M mModel;
protected V mView;
public BasePresenter(M mModel, V mView) {
this.mModel = mModel;
this.mView = mView;
}
@Override
public void destory() {
}
}
P层作为中间层,应获取M,V的对象
V层(视图层)
IView
public interface IView {
void showLoading();
void hideLoading();
void showToast(String msg);
}
IActivity
public interface IActivity {
int bandLayout();
void initView();
void initData();
}
BaseActivity
public abstract class BaseActivity<P extends IPresenter> extends AppCompatActivity implements IView,IActivity {
protected P mPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(bandLayout());
initView();
initData();
}
@Override
public void showLoading() {
}
@Override
public void hideLoading() {
}
@Override
public void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
IFragment
public interface IFragment extends IActivity {
<V extends View> V findViewById(int id);
}
BaseFragment
public abstract class BaseFragment<P extends IPresenter> extends Fragment implements IView, IFragment {
protected P mPersenter;
private View mBaseView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return mBaseView = inflater.inflate(bandLayout(), container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView();
initData();
}
@Override
public <V extends View> V findViewById(int id) {
return mBaseView.findViewById(id);
}
@Override
public void showLoading() {
}
@Override
public void hideLoading() {
}
@Override
public void showToast(String msg) {
Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
}
}
NetWork网络层,封装网络获取数据的内容
单例模式
retrofit进行封装
public class NetWorkManager {
private NetWorkManager() {
}
private static volatile NetWorkManager manager = new NetWorkManager();
public static synchronized NetWorkManager getNetWorkManager() {
return manager;
}
private Retrofit retrofit;
public Retrofit getRetrofit() {
if (retrofit == null) {
create();
}
return retrofit;
}
private void create() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
okBuilder.addInterceptor(interceptor)
.readTimeout(60 * 1000, TimeUnit.MILLISECONDS)
.writeTimeout(60 * 1000, TimeUnit.MILLISECONDS)
.connectTimeout(60 * 1000, TimeUnit.MILLISECONDS);
Retrofit.Builder builder = new Retrofit.Builder();
retrofit = builder.baseUrl("http://www.qubaobei.com/ios/")
.client(okBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
}