首先:两个布局:activity_main和item_mvp
activity_main:布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
其次ltem_mvp布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/head"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="content" />
</LinearLayout>
定义一个适配器:MVPAdapter:
public class MVPAdapter extends BaseAdapter {
List<MVP> list;
public MVPAdapter(Context context, List<MVP> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.item_mvp, parent, false);
holder.head = (ImageView) convertView.findViewById(R.id.head);
holder.content = (TextView) convertView.findViewById(R.id.content);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
MVP girl = list.get(position);
holder.head.setImageResource(girl.getHead());
holder.content.setText(girl.getContent());
return convertView;
}
static class ViewHolder {
TextView content;
ImageView head;
}
}
定义一个实体类:MVP:
public class MVP {
private int head;
private String content;
public MVP(int head, String content) {
super();
this.head = head;
this.content = content;
}
public int getHead() {
return head;
}
public void setHead(int head) {
this.head = head;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
首先第一种情况没有使用mvp,只在MainActivity中建立代码:
public class MainActivity extends Activity {
private ListView listview;
private List<Girl> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
}
private void initData() {
listview=(ListView) findViewById(R.id.listview);
list=new ArrayList<Girl>();
list.add(new Girl(R.drawable.image1, "你是我的小苹果image1"));
list.add(new Girl(R.drawable.image2, "你是我的小苹果image2"));
list.add(new Girl(R.drawable.image3, "你是我的小苹果image3"));
list.add(new Girl(R.drawable.image4, "你是我的小苹果image4"));
list.add(new Girl(R.drawable.image5, "你是我的小苹果image5"));
list.add(new Girl(R.drawable.image6, "你是我的小苹果image6"));
list.add(new Girl(R.drawable.image7, "你是我的小苹果image7"));
list.add(new Girl(R.drawable.image8, "你是我的小苹果image8"));
Toast.makeText(getApplicationContext(), "正在加载", 1).show();
listview.setAdapter(new GirlAdapter(this, list));
}
//1.数据加载前需要显示的dialog
//2.加载后绑定数据listview.setAdapter()
}
第二种情况:使用MVP
首先简单介绍一下:MVC:M->网络或者数据库,V->界面,C->activity
MVP: M——>网络/数据库,V——>界面, P——>activity进一步优化
第一步:将ArrayList所有方法提取出来(可以看成网络数据)
①定义一个接口Model接口
public interface MVPModel {
void loadGirl(onGirlListener listener);
interface onGirlListener {
void onComplete(ArrayList<MVP> list);// 加载完成时
}
}
②建立一个Model将ArrayList添加进去:
public class MVPModelImpl implements MVPModel{
@Override
public void loadGirl(onGirlListener listener) {
ArrayList<MVP> list = new ArrayList<MVP>();
list.add(new MVP(R.drawable.image1, "你是我的小苹果image1"));
list.add(new MVP(R.drawable.image2, "你是我的小苹果image2"));
list.add(new MVP(R.drawable.image3, "你是我的小苹果image3"));
list.add(new MVP(R.drawable.image4, "你是我的小苹果image4"));
list.add(new MVP(R.drawable.image5, "你是我的小苹果image5"));
list.add(new MVP(R.drawable.image6, "你是我的小苹果image6"));
list.add(new MVP(R.drawable.image7, "你是我的小苹果image7"));
list.add(new MVP(R.drawable.image8, "你是我的小苹果image8"));
//返回数据
listener.onComplete(list);
}
//activity中方法想被调用用接口
}
第二步,将toast方法和listview加载绑定适配器抽取一个方法
public interface MVPView {
void showDialog();
void showGirls(List<MVP> list);
}
第三步:使用Presenter将model和view进行绑定
public class MVPPresenter {
// 初始化view
MVPView girlView;
// 初始化model
MVPModel model = new MVPModelImpl();
public MVPPresenter(MVPView girlView) {
this.girlView = girlView;
}
public void fech() {
if (girlView != null && model != null) {
girlView.showDialog();
SystemClock.sleep(2000);
//加载数据
model.loadGirl(new onGirlListener() {
@Override
public void onComplete(ArrayList<MVP> list) {
girlView.showGirls(list);
}
});
}
}
}
最后一步MainActivity:
public class MainActivity extends Activity implements MVPView {
private ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
new MVPPresenter(this).fech();
}
private void initData() {
listview = (ListView) findViewById(R.id.listview);
}
// 1.数据加载前需要显示的dialog
@Override
public void showDialog() {
Toast.makeText(getApplicationContext(), "正在加载", 1).show();
}
// 2.加载后绑定数据listview.setAdapter()
@Override
public void showGirls(List<MVP> list) {
listview.setAdapter(new MVPAdapter(this, list));
}
}