结合前一篇MVC框架模式
为了更好地细分视图(View)与模型(Model)的功能,让View专注于处理数据的可视化以及与用户的交互,同时让Model只关系数据的处理,基于MVC概念的MVP(Model-View-Presenter)模式应运而生。
在MVP模式里通常包含4个要素:
(1)View:负责绘制UI元素、与用户进行交互(在Android中体现为Activity);
(2)View interface:需要View实现的接口,View通过View interface与Presenter进行交互,降低耦合,方便进行单元测试;
(3)Model:负责存储、检索、操纵数据(有时也实现一个Model interface用来降低耦合);
(4)Presenter:作为View与Model交互的中间纽带,处理与用户交互的负责逻辑。
MVP模式步骤:
1、创建一个IView让Presenter去依赖接口,不管数据时从哪里来,只要提供接口。
2、让Activity或者Fragment去实现接口,实现所有View的操作。
3、创建主持人,注入IView的接口,数据都从接口中获取。
4、创建Model,实现数据加载(网络、数据路、假数据)
5、使用主持人访问Model,获取数据后,调用IView去刷新布局。
还是使用藏头诗接口的例子
布局文件
1 xmlns:tools="http://schemas.android.com/tools" 2 android: 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context="com.example.lesson_mvc_cangtoushi.ui.MainActivity"> 7 8 <RadioGroup 9 android:id="@+id/rg_57" 10 android:layout_width="match_parent" 11 android:layout_height="wrap_content" 12 android:orientation="horizontal"> 13 14 <RadioButton 15 android:id="@+id/rb_5" 16 android:layout_width="0dp" 17 android:layout_height="wrap_content" 18 android:layout_weight="1" 19 android:gravity="center" 20 android:text="五言诗" /> 21 22 <RadioButton 23 android:id="@+id/rb_7" 24 android:layout_width="0dp" 25 android:layout_height="wrap_content" 26 android:layout_weight="1" 27 android:gravity="center" 28 android:text="七言诗" /> 29 </RadioGroup> 30 31 <RadioGroup 32 android:id="@+id/rg_ct" 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content" 35 android:orientation="horizontal"> 36 37 <RadioButton 38 android:id="@+id/rb_ct" 39 android:layout_width="0dp" 40 android:layout_height="wrap_content" 41 android:layout_weight="1" 42 android:gravity="center" 43 android:text="藏头" /> 44 45 <RadioButton 46 android:id="@+id/rb_cw" 47 android:layout_width="0dp" 48 android:layout_height="wrap_content" 49 android:layout_weight="1" 50 android:gravity="center" 51 android:text="藏尾" /> 52 53 <RadioButton 54 android:id="@+id/rb_cz" 55 android:layout_width="0dp" 56 android:layout_height="wrap_content" 57 android:layout_weight="1" 58 android:gravity="center" 59 android:text="藏中" /> 60 61 <RadioButton 62 android:id="@+id/rb_dz" 63 android:layout_width="0dp" 64 android:layout_height="wrap_content" 65 android:layout_weight="1" 66 android:gravity="center" 67 android:text="递增" /> 68 69 <RadioButton 70 android:id="@+id/rb_dj" 71 android:layout_width="0dp" 72 android:layout_height="wrap_content" 73 android:layout_weight="1" 74 android:gravity="center" 75 android:text="递减"/> 76 77 </RadioGroup> 78 79 <RadioGroup 80 android:id="@+id/rg_yy" 81 android:layout_width="match_parent" 82 android:layout_height="wrap_content" 83 android:orientation="horizontal"> 84 85 <RadioButton 86 android:id="@+id/rb_1y" 87 android:layout_width="0dp" 88 android:layout_height="wrap_content" 89 android:layout_weight="1" 90 android:gravity="center" 91 android:text="双句一押" /> 92 93 <RadioButton 94 android:id="@+id/rb_2y" 95 android:layout_width="0dp" 96 android:layout_height="wrap_content" 97 android:layout_weight="1" 98 android:gravity="center" 99 android:text="双句押韵" /> 100 101 <RadioButton 102 android:id="@+id/rb_3y" 103 android:layout_width="0dp" 104 android:layout_height="wrap_content" 105 android:layout_weight="1" 106 android:gravity="center" 107 android:text="一三四押" /> 108 </RadioGroup> 109 <EditText 110 android:id="@+id/et_key" 111 android:layout_width="match_parent" 112 android:layout_height="wrap_content" 113 android:hint="请输入藏头诗"/> 114 <Button 115 android:id="@+id/btn_submit" 116 android:layout_width="match_parent" 117 android:layout_height="wrap_content" 118 android:text="提交"/> 119 120 <ScrollView 121 android:layout_width="match_parent" 122 android:layout_height="match_parent"> 123 <TextView 124 android:id="@+id/tv_show" 125 android:layout_width="match_parent" 126 android:layout_height="match_parent" /> 127 </ScrollView> 128 129 130 131 </LinearLayout>
java代码目录结构。可以看出MVP多了一个Presenter,主持人,还有一个View接口,极大的实现了解耦
藏头诗对象原型bean
1 public class CangTouShiBean { 2 3 4 /** 5 * showapi_res_code : 0 6 * showapi_res_error : 7 * showapi_res_body : {"ret_code":0,"list":["北风勇士马,晚水独芙蓉。吾将宝非宝,英雄徒自强。","朝骑五花马,太华三芙蓉。吾将宝非宝,天子贵文强。","请歌牵白马,菡萏金芙蓉。大位天下宝,自从冒顿强。","青丝系五马,秀出九芙蓉。迈德惟家宝,日来知自强。","北买党项马,美女夸芙蓉。河宗来献宝,十年思自强。","青丝系五马,大嫂采芙蓉。药妙灵仙宝,不独有文强。"]} 8 */ 9 10 private int showapi_res_code; 11 private String showapi_res_error; 12 private ShowapiResBodyBean showapi_res_body; 13 14 15 @Override 16 public String toString() { 17 return "CangTouShiBean{" + 18 "showapi_res_code=" + showapi_res_code + 19 ", showapi_res_error='" + showapi_res_error + '\'' + 20 ", showapi_res_body=" + showapi_res_body + 21 '}'; 22 } 23 24 public int getShowapi_res_code() { 25 return showapi_res_code; 26 } 27 28 public void setShowapi_res_code(int showapi_res_code) { 29 this.showapi_res_code = showapi_res_code; 30 } 31 32 public String getShowapi_res_error() { 33 return showapi_res_error; 34 } 35 36 public void setShowapi_res_error(String showapi_res_error) { 37 this.showapi_res_error = showapi_res_error; 38 } 39 40 public ShowapiResBodyBean getShowapi_res_body() { 41 return showapi_res_body; 42 } 43 44 public void setShowapi_res_body(ShowapiResBodyBean showapi_res_body) { 45 this.showapi_res_body = showapi_res_body; 46 } 47 48 public static class ShowapiResBodyBean { 49 /** 50 * ret_code : 0 51 * list : ["北风勇士马,晚水独芙蓉。吾将宝非宝,英雄徒自强。","朝骑五花马,太华三芙蓉。吾将宝非宝,天子贵文强。","请歌牵白马,菡萏金芙蓉。大位天下宝,自从冒顿强。","青丝系五马,秀出九芙蓉。迈德惟家宝,日来知自强。","北买党项马,美女夸芙蓉。河宗来献宝,十年思自强。","青丝系五马,大嫂采芙蓉。药妙灵仙宝,不独有文强。"] 52 */ 53 54 private int ret_code; 55 private List<String> list; 56 57 58 @Override 59 public String toString() { 60 return "ShowapiResBodyBean{" + 61 "ret_code=" + ret_code + 62 ", list=" + list + 63 '}'; 64 } 65 66 public int getRet_code() { 67 return ret_code; 68 } 69 70 public void setRet_code(int ret_code) { 71 this.ret_code = ret_code; 72 } 73 74 public List<String> getList() { 75 return list; 76 } 77 78 public void setList(List<String> list) { 79 this.list = list; 80 } 81 } 82 }
藏头诗Model实现藏头诗接口,请求数据,将结果回调出去
1 public interface BeanCallback<T> { 2 3 void onError(String msg); 4 void onSuccess(T t); 5 }