package com.example.mvpproject;
import java.util.List;
import com.example.mvpproject.adapter.GirlListAdapter;
import com.example.mvpproject.bean.Girl;
import com.example.mvpproject.presenter.GirlsPresenter;
import com.example.mvpproject.viewinterface.IGirlsView;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends BaseActivity> implements IGirlsView{
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_v1);
listview=(ListView) findViewById(R.id.listview);
// new GirlsPresenter(this).fetch();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void loadGirls() {
Toast.makeText(this, "数据加载中请稍后...", Toast.LENGTH_SHORT).show();
}
@Override
public void showGirls(List girls) {
GirlListAdapter adapter=new GirlListAdapter(this, girls);
listview.setAdapter(adapter);
}
@Override
public GirlsPresenter createPresenter() {
return new GirlsPresenter(this);
}
}
package com.example.mvpproject;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import com.example.mvpproject.presenter.BasePresenter;
public abstract class BaseActivity> extends ActionBarActivity {
public T mPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mPresenter=createPresenter();
mPresenter.attatchView((V)this);
mPresenter.fetch();
}
@Override
protected void onDestroy() {
super.onDestroy();
mPresenter.depatch();
}
public abstract T createPresenter();
}
BaseActivity为基类,主要作用一是解绑,目的是在所有Activity销毁时,调用Presenter的depatch方法进行解绑,避免activity内存泄漏;而是Presenter的初始化
MainActivity中则实现了IGirlsView的接口,接口中主要有俩UI逻辑方法,分别对应数据加载时的Toast提示,和具体的listview展示
ViewInterfaces代码:
package com.example.mvpproject.viewinterface;
import java.util.List;
import com.example.mvpproject.bean.Girl;
public interface IGirlsView {
void loadGirls();
void showGirls(List girls);
}
Presenter层代码:
package com.example.mvpproject.presenter;
import java.util.List;
import com.example.mvpproject.bean.Girl;
import com.example.mvpproject.model.GirlsModelImp;
import com.example.mvpproject.model.IGirlsModel.OnGirlsResponse;
import com.example.mvpproject.viewinterface.IGirlsView;
/**
* 业务逻辑处理层(持有View接口和model层的引用)
* @author zhengcheng
*
*/
public class GirlsPresenter extends BasePresenter{
private IGirlsView girlsView;
private GirlsModelImp girlsModelImp;
public GirlsPresenter(IGirlsView girlsView) {
super();
this.girlsView = girlsView;
girlsModelImp=new GirlsModelImp();
}
@Override
public void fetch() {
if(girlsModelImp!=null){
girlsView.loadGirls();
girlsModelImp.loadData(new OnGirlsResponse() {
@Override
public void complete(List girls) {
girlsView.showGirls(girls);
}
});
}
}
}
package com.example.mvpproject.presenter;
import java.lang.ref.WeakReference;
public abstract class BasePresenter {
protected WeakReference weakRef;
public void attatchView(T view){
weakRef=new WeakReference(view);
}
/**
* 解绑
*/
public void depatch(){
if(weakRef!=null){
weakRef.clear();
weakRef=null;
}
}
/**
* 数据请求
*/
public abstract void fetch();
}
presenter层主要是处理具体的UI逻辑,所以他既有View层的引用又拥有Model层的引用,通过构造函数将viewinterface传入,和对应的model初始化,fetch方法为绑定数据的方法。
basepresenter类为presenter类的基类,核心方法为depatch解绑方法,该方法中将接口设置为弱引用,为的就是当Activity销毁时,与presenter的完全解绑,防止内存泄漏
Model层代码:
package com.example.mvpproject.model;
import java.util.ArrayList;
import java.util.List;
import com.example.mvpproject.R;
import com.example.mvpproject.bean.Girl;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
/**
* model层 数据逻辑处理层
* @author zhengcheng
*
*/
public class GirlsModelImp implements IGirlsModel {
private Handler handler=new Handler(Looper.getMainLooper());
@Override
public void loadData(final OnGirlsResponse onGirlsResponse) {
//数据加载
new Thread(){
public void run() {
SystemClock.sleep(3000);
final List data = new ArrayList();
data.add(new Girl(R.drawable.f1, "五颗星", "佰仟媚儿初夏新款韩版时尚潮流女个性字母印花无袖露脐上衣"));
data.add(new Girl(R.drawable.f2, "四颗星", "迷依诗诺夏天新款韩版女装性感夜店欧美风字母印花牛仔露脐背心上衣"));
data.add(new Girl(R.drawable.f3, "五颗星", "迷依诗诺春夏欧美新款性感女装单排扣牛仔背心露脐上衣"));
data.add(new Girl(R.drawable.f4, "三颗星", "美莉丹 新款欧美单排扣牛仔背心露脐上衣"));
data.add(new Girl(R.drawable.f5, "五颗星", "夏季新款韩版时尚个性字母无袖露脐上衣"));
data.add(new Girl(R.drawable.f6, "三颗星", "新款欧美单排扣牛仔背心露脐上衣"));
data.add(new Girl(R.drawable.f7, "四颗星", "夏装新款下摆波浪边挂脖露肩"));
data.add(new Girl(R.drawable.f8, "五颗星", "夏天新款欧美时尚潮流休闲百"));
data.add(new Girl(R.drawable.f9, "四颗星", "迷依诗诺夏季新款小香风甜美性感夜"));
data.add(new Girl(R.drawable.f10, "三颗星", "安巴克夏季新款韩版时尚套装性感"));
handler.post(new Runnable() {
@Override
public void run() {
onGirlsResponse.complete(data);
}
});
};
}.start();
}
}
这样一个完整的MVP流程就走完了,其他接口/adapter和bean相关代码如下
package com.example.mvpproject.model;
import java.util.List;
import com.example.mvpproject.bean.Girl;
public interface IGirlsModel {
void loadData(OnGirlsResponse onGirlsResponse);
public interface OnGirlsResponse{
void complete(List girls);
}
}
package com.example.mvpproject.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import com.example.mvpproject.R;
import com.example.mvpproject.bean.Girl;
public class GirlListAdapter extends BaseAdapter {
private LayoutInflater inflater;
private List data;
public GirlListAdapter(Context context, List data) {
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = inflater.inflate(R.layout.item_v1, null);
Girl g = data.get(position);
ImageView iv_icon = (ImageView) view.findViewById(R.id.iv_icon);
iv_icon.setImageResource(g.icon);
TextView tv_like = (TextView) view.findViewById(R.id.tv_like);
tv_like.setText("星级 "+g.like);
TextView tv_style = (TextView) view.findViewById(R.id.tv_style);
tv_style.setText(g.style);
return view;
}
}
package com.example.mvpproject.bean;
public class Girl {
public int icon;
public String like;
public String style;
public Girl(int icon, String like, String style) {
this.icon = icon;
this.like = like;
this.style = style;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
}