代碼源:https://github.com/lijingchang/smallMVP.git
google官方示例代碼有點(diǎn)復(fù)雜拓挥,所以自己寫了一個(gè)demo嵌言。
View:
public class MainActivity extends Activity implements IView { private EditText shelltext; private TextView backon; private Ipresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); presenter=new Testpresenter(this); shelltext=(EditText)findViewById(R.id.cmd); backon=(TextView)findViewById(R.id.backon); findViewById(R.id.doshell).setOnClickListener(myhandler); findViewById(R.id.showpos).setOnClickListener(myhandler); findViewById(R.id.showall).setOnClickListener(myhandler); backon.setMovementMethod(ScrollingMovementMethod.getInstance()); } View.OnClickListener myhandler = new View.OnClickListener() { public void onClick(View v) { switch(v.getId()) { case R.id.doshell: presenter.add(shelltext.getText().toString()); break; case R.id.showpos: presenter.showpos(shelltext.getText().toString()); break; case R.id.showall: presenter.showall(); break; default: break; } } }; @Override public void settext(String arg1){ backon.append(arg1+'\n'); } @Override public void setEdittext(String arg1){ shelltext.setText(arg1); } }
完成IView定義函數(shù)玩祟,將之傳給presenter圣贸。IView定義就是@Override里面的幾個(gè)函數(shù)
Presenter
package com.example.presenter; import com.example.model.DataModel; import com.example.model.IModel; import com.example.test.IView; public class Testpresenter implements Ipresenter{ private IView viewcontrol; private IModel model; public Testpresenter(IView view){ this.viewcontrol=view; this.model=new DataModel(); } @Override public void add(String arg0){ model.additem(arg0); } @Override public void showpos(String arg0){ viewcontrol.settext(""+model.showpos(arg0)); } @Override public void showall(){ viewcontrol.settext(""+model.showall()); } }
接收View傳給的IView調(diào)用方法岖赋,完成Ipresenter定義函數(shù)映挂,初始化Model。
Model
package com.example.model; import java.util.ArrayList; import java.util.List; public class DataModel implements IModel{ private List<String> cmdlist=new ArrayList<String>(); @Override public void additem(String arg1){ cmdlist.add(arg1); } @Override public void additem(String arg1,int pos){ cmdlist.add(pos, arg1); } @Override public int getcount(){ return cmdlist.size(); } @Override public String getindexof(int pos){ return cmdlist.get(pos); } @Override public String showall(){ return cmdlist.toString(); } @Override public void clear(){ cmdlist.clear(); } @Override public int showpos(String arg1){ int i=cmdlist.indexOf(arg1); return i; } }
完成IModel定義函數(shù)蝶押,寫數(shù)據(jù)接口
Interface
public interface IModel { void additem(String arg1); void additem(String arg1,int pos); int getcount(); String getindexof(int pos); void clear(); int showpos(String arg1); String showall(); }
package com.example.presenter; import com.example.model.DataModel; import com.example.model.IModel; import com.example.test.IView; public class Testpresenter implements Ipresenter{ private IView viewcontrol; private IModel model; public Testpresenter(IView view){ this.viewcontrol=view; this.model=new DataModel(); } @Override public void add(String arg0){ model.additem(arg0); } @Override public void showpos(String arg0){ viewcontrol.settext(""+model.showpos(arg0)); } @Override public void showall(){ viewcontrol.settext(""+model.showall()); } }
public interface IView { void settext(String arg1); void setEdittext(String arg1); }