mvp
一款好用的架构模式会是代码更加解偶,方便查看修改,所以mvp模式越来越流行,而我一直都在观望,只是通过别人写的文章来看看,自己的实际项目中并没有使用过,所以每次回想都感觉不会,现在下定决心这次一定要学会,可以一步一步来实现,我一般是看别人的博客边看边敲,最后自己总结一下
顾名思义,mvp分为m(model),v(view),p(presenter)三层,三者之间的关系是这样的,presenter是view和model之间的桥梁,view不能直接对model进行操作,具体实现的时候是把Activity中的UI逻辑抽象成view接口,model层进行网络请求等,presenter是业务逻辑抽象成presenter的接口
实践是检查真理的唯一标准。不能光看不练,这样永远都感觉会却到实际项目中不知如何下手。
以下是我自己写的demo请求聚合数据用recyclerview展示
图片是我这个项目的目录结构
接下来就一一介绍
首先是model,包含两个类,NewsDataBean是实体类,用来展示数据的
MainModel是逻辑请求,贴出来代码块
public class MainModel {
private IMainPresenter mainPresenter;
public MainModel(IMainPresenter presenter) {
this.mainPresenter = presenter;
public void loadData() {
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.get("http://v.juhe.cn/toutiao/index?type=top&key=47ac9dcad443ae961e4fe27b0eb8ddf2", new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
Log.e("MainModel", response.toString());
ArrayList<NewsDataBean> list = new ArrayList<>();
try {
JSONArray data = response.getJSONObject("result").getJSONArray("data");
for (int i = 0; i < data.length(); i++) {
JSONObject object = data.getJSONObject(i);
String title = object.getString("title");
String date = object.getString("date");
String category = object.getString("category");
String author_name = object.getString("author_name");
String url = object.getString("url");
String thumbnail_pic_s = object.getString("thumbnail_pic_s");
NewsDataBean bean = new NewsDataBean(title, date, category, author_name, url, thumbnail_pic_s);
list.add(bean);
}
} catch (JSONException e) {
e.printStackTrace();
}
mainPresenter.loadDataSuccess(list);
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
mainPresenter.loadDataFail();
}
});
}}
view包下的MainView
public interface MainView {
void showData(ArrayList<NewsDataBean> list);
}
presenter包下有两个接口一个类
public interface Presenter<V> {
void attachView(V view);
void detachView();
}
IMainPresenter接口
public interface IMainPresenter {
void loadDataSuccess(ArrayList<NewsDataBean> list);
void loadDataFail();
}
MainPresenter类
public class MainPresenter implements Presenter<MainView>, IMainPresenter {
private MainView mainView;
private final MainModel mMainModel;
public MainPresenter(MainView mainView) {
attachView(mainView);
mMainModel = new MainModel(this);
}
public void loadData() {
mMainModel.loadData();//加载数据
}
@Override
public void loadDataSuccess(ArrayList<NewsDataBean> list) {
mainView.showData(list);
}
@Override
public void loadDataFail() {
}
@Override
public void attachView(MainView view) {
this.mainView = view;
}
@Override
public void detachView() {
this.mainView = null;
}
}
接下来就是Activity了
public class MainActivity extends AppCompatActivity implements MainView {//实现Mainview
private ArrayList<String> list = new ArrayList<>();
private static final String TAG = "MainActivity";
private ImageView image;
private Adapter adapter;
private MainPresenter presenter;
private RecyclerView rv;
private SwipeRefreshLayout refreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
presenter = new MainPresenter(this);
presenter.loadData();//通知Presenter加载数据
setContentView(R.layout.activity_main);
initView();
}
@Override
protected void onResume() {
super.onResume();
}
private void initView() {
rv = findViewById(R.id.rv);
refreshLayout = findViewById(R.id.refresh);
LinearLayoutManager manager = new LinearLayoutManager(this);
rv.setLayoutManager(manager);
}
@Override
public void showData(final ArrayList<NewsDataBean> list) {
Log.e(TAG, "showData: " + list.size());
adapter = new Adapter(list, MainActivity.this);
adapter.setMyListener(new Adapter.onItemClickListener() {
@Override
public void onItemClickListener(int position) {
Toast.makeText(MainActivity.this, "this is" + position, Toast.LENGTH_SHORT).show();
}
});
rv.setAdapter(adapter);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
presenter.loadData();
adapter.notifyDataSetChanged();
refreshLayout.setRefreshing(false);
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detachView();
}
}
从代码中来看还是很清晰的,其中presenter包中的接口有两个,当然这只是一个思路,可以根据自己的需求来设计。
写着写着就发现了一个问题,这只是一个Activity,如果有多个的话,是不是就得定义多个model,view,presenter呢?其实不然,我们可以抽成basemvp,这个我之后会写。
下一篇是mvp+Retrofit+RxJava.