├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── haibuzou │ │ └── mvpsample │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── haibuzou │ │ │ └── mvpsample │ │ │ ├── MainActivity.java │ │ │ ├── basemvp │ │ │ ├── BaseMvpActivity.java │ │ │ ├── BasePresenter.java │ │ │ ├── BaseView.java │ │ │ └── NewBasePresenter.java │ │ │ ├── biz │ │ │ ├── OnRequestListener.java │ │ │ ├── RequestBiz.java │ │ │ └── RequestBiziml.java │ │ │ ├── mvc │ │ │ └── MVCActivity.java │ │ │ ├── mvp │ │ │ ├── MVPActivity.java │ │ │ ├── presenter │ │ │ │ └── MvpPresenter.java │ │ │ └── view │ │ │ │ └── MvpView.java │ │ │ └── newmvp │ │ │ ├── NewMvpActivity.java │ │ │ ├── NewMvpPresenter.java │ │ │ └── NewMvpView.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_mvc.xml │ │ └── activity_mvp.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── haibuzou │ └── mvpsample │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | 15 | # Gradle files 16 | .gradle/ 17 | build/ 18 | 19 | # Local configuration file (sdk path, etc) 20 | local.properties 21 | 22 | # Proguard folder generated by Eclipse 23 | proguard/ 24 | 25 | # IntelliJ files 26 | .idea 27 | *.iml 28 | 29 | # Maven output folder 30 | target 31 | 32 | # Misc 33 | .DS_Store 34 | Thumbs.db 35 | *.swp 36 | *.bak -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MVPSample 2 | 一点点MVP的个人心得 3 | 博客介绍: http://blog.csdn.net/dantestones/article/details/50899235 4 | 5 | ## MVC VS MVP 6 | 同样的功能使用MVC和MVP2种实现方式,比较才能更好的理解 7 | 8 | ![](http://img.blog.csdn.net/20160317163144720) 9 | 10 | 11 | ## MVC架构 12 | - M : 业务层和模型层,相当与javabean和我们的业务请求代码 13 | - V : 视图层,对应Android的layout.xml布局文件 14 | - C : 控制层,对应于Activity中对于UI 的各种操作 15 | 16 | ## MVP架构 17 | - M : 还是业务层和模型层 18 | - V : 视图层的责任由Activity来担当 19 | - P : 新成员Presenter 用来代理 C(control) 控制层 20 | 21 | ## MVP通用类 22 | 新增了MVP通用类,更少的代码,更好使用 23 | * BaseView 24 | * BasePresenter 25 | * BaseMvpActivity 26 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion '26.0.2' 6 | 7 | defaultConfig { 8 | applicationId "haibuzou.mvpsample" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.2.0' 26 | } 27 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\studio\android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/haibuzou/mvpsample/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | 8 | import haibuzou.mvpsample.mvc.MVCActivity; 9 | import haibuzou.mvpsample.mvp.MVPActivity; 10 | import haibuzou.mvpsample.newmvp.NewMvpActivity; 11 | 12 | public class MainActivity extends AppCompatActivity { 13 | 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.activity_main); 18 | } 19 | 20 | public void toMvc(View view){ 21 | startActivity(new Intent(MainActivity.this, MVCActivity.class)); 22 | } 23 | 24 | public void toMvp(View view){ 25 | startActivity(new Intent(MainActivity.this, MVPActivity.class)); 26 | } 27 | 28 | public void toNewMvp(View view){ 29 | startActivity(new Intent(MainActivity.this, NewMvpActivity.class)); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/basemvp/BaseMvpActivity.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.basemvp; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | 6 | public abstract class BaseMvpActivity> extends AppCompatActivity { 7 | 8 | public T presenter; 9 | 10 | @Override 11 | protected void onCreate(Bundle savedInstanceState) { 12 | super.onCreate(savedInstanceState); 13 | presenter = initPresenter(); 14 | } 15 | 16 | @Override 17 | protected void onResume() { 18 | super.onResume(); 19 | presenter.attach((V)this); 20 | } 21 | 22 | @Override 23 | protected void onDestroy() { 24 | presenter.dettach(); 25 | super.onDestroy(); 26 | } 27 | 28 | public abstract T initPresenter(); 29 | 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/basemvp/BasePresenter.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.basemvp; 2 | 3 | 4 | public abstract class BasePresenter { 5 | public T mView; 6 | 7 | public void attach(T mView){ 8 | this.mView = mView; 9 | } 10 | 11 | public void dettach(){ 12 | mView = null; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/basemvp/BaseView.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.basemvp; 2 | 3 | 4 | public interface BaseView { 5 | 6 | void showLoading(); 7 | void hideLoading(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/basemvp/NewBasePresenter.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.basemvp; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.content.Context; 6 | import android.util.Log; 7 | 8 | import java.lang.ref.WeakReference; 9 | import java.lang.reflect.InvocationHandler; 10 | import java.lang.reflect.Method; 11 | import java.lang.reflect.ParameterizedType; 12 | import java.lang.reflect.Proxy; 13 | import java.lang.reflect.Type; 14 | 15 | import haibuzou.mvpsample.mvp.view.MvpView; 16 | 17 | /** 18 | * Created by haibuzou on 2017/3/29. 19 | * simple哥的Presenter基类,解决类View释放导致可能的空指针问题 20 | * 21 | * 22 | * Mvp Presenter 抽象类. 通过 弱引用持有 Context 和 View对象, 避免产生内存泄露。 23 | * 24 | * 注意, 如果Presenter有多个泛型类,那么 MvpView类型的泛型类要放在第一位. 25 | * 26 | *

27 | * 当 Context (通常是指Activity)被销毁时如果客户端程序 28 | * 再调用Context, 那么直接返回 Application 的Context. 因此如果用户需要调用与Activity相关的UI操作(例如弹出Dialog)时, 29 | * 应该先调用 {@link #isActivityAlive()} 来判断Activity是否还存活. 30 | *

31 | * 32 | *

33 | * 当 View 对象销毁时如果用户再调用 View对象, 那么则会 34 | * 通过动态代理创建一个View对象 {@link #mNullViewProxy}, 这样保证 view对象不会为空. 35 | * 使用getMvpView方法来获取View 36 | *

37 | */ 38 | 39 | 40 | public class NewBasePresenter { 41 | /** 42 | * Null Mvp View InvocationHandler 43 | */ 44 | private static final InvocationHandler NULL_VIEW = new MvpViewInvocationHandler(); 45 | /** 46 | * application context 47 | */ 48 | private static Context sAppContext; 49 | /** 50 | * context weak reference 51 | */ 52 | private WeakReference mContextRef; 53 | /** 54 | * mvp view weak reference 55 | */ 56 | private WeakReference mViewRef; 57 | /** 58 | * mvp view class 59 | */ 60 | private Class mMvpViewClass = null; 61 | /** 62 | * Mvp View created by dynamic Proxy 63 | */ 64 | private T mNullViewProxy; 65 | /** 66 | * init application context with reflection. 67 | */ 68 | static { 69 | try { 70 | // 先通过 ActivityThread 来获取 Application Context 71 | Application application = (Application) Class.forName("android.app.ActivityThread").getMethod 72 | ("currentApplication").invoke(null, (Object[]) null); 73 | if (application != null) { 74 | sAppContext = application; 75 | } 76 | if (sAppContext == null) { 77 | // 第二种方式初始化 78 | application = (Application) Class.forName("android.app.AppGlobals").getMethod("getInitialApplication") 79 | .invoke(null, (Object[]) null); 80 | if (application != null) { 81 | sAppContext = application; 82 | } 83 | } 84 | } catch (Exception e) { 85 | e.printStackTrace(); 86 | } 87 | } 88 | 89 | 90 | public NewBasePresenter() { 91 | } 92 | 93 | 94 | public NewBasePresenter(Context context, T view) { 95 | attach(context, view); 96 | } 97 | 98 | /** 99 | * attach context & mvp view 100 | * @param context 101 | * @param view 102 | */ 103 | public void attach(Context context, T view) { 104 | mContextRef = new WeakReference<>(context); 105 | mViewRef = new WeakReference<>(view); 106 | if (sAppContext == null && context != null) { 107 | sAppContext = context.getApplicationContext(); 108 | } 109 | } 110 | 111 | 112 | /** 113 | * release resource 114 | */ 115 | public void detach() { 116 | if ( mContextRef != null ) { 117 | mContextRef.clear(); 118 | } 119 | mContextRef = null; 120 | if ( mViewRef != null ) { 121 | mViewRef.clear(); 122 | } 123 | mViewRef = null; 124 | } 125 | 126 | /** 127 | * UI展示相关的操作需要判断一下 Activity 是否已经 finish. 128 | *

129 | * todo : 只有当 isActivityAlive 返回true时才可以执行与Activity相关的操作, 130 | * 比如 弹出Dialog、Window、跳转Activity等操作. 131 | * 132 | * @return 133 | */ 134 | protected boolean isActivityAlive() { 135 | return !isActivityFinishing() && mViewRef.get() != null; 136 | } 137 | 138 | 139 | /** 140 | * 返回 Context. 如果 Activity被销毁, 那么返回应用的Context. 141 | * 142 | * 注意: 143 | * 通过过Context进行UI方面的操作时应该调用 {@link #isActivityAlive()} 144 | * 判断Activity是否还已经被销毁, 在Activity未销毁的状态下才能操作. 否则会引发crash. 145 | * 而获取资源等操作则可以使用应用的Context. 146 | * 147 | * @return 148 | */ 149 | protected Context getContext() { 150 | Context context = mContextRef != null ? mContextRef.get() : null; 151 | if (context == null || isActivityFinishing()) { 152 | context = sAppContext; 153 | } 154 | return context; 155 | } 156 | 157 | 158 | protected String getString(int rid) { 159 | return getContext().getString(rid); 160 | } 161 | 162 | 163 | /** 164 | * activity 是否是finishing状态 165 | * 166 | * @return 167 | */ 168 | private boolean isActivityFinishing() { 169 | if (mContextRef == null) { 170 | return true; 171 | } 172 | Context context = mContextRef.get(); 173 | if (context instanceof Activity) { 174 | Activity hostActivity = (Activity) context; 175 | return hostActivity.isFinishing(); 176 | } 177 | return true; 178 | } 179 | 180 | /** 181 | * 返回 Mvp View对象. 如果真实的 View对象已经被销毁, 那么会通过动态代理构建一个View, 182 | * 确保调用 View对象执行操作时不会crash. 183 | * @return Mvp View 184 | */ 185 | protected T getMvpView() { 186 | T view = mViewRef != null ? mViewRef.get() : null; 187 | if (view == null) { 188 | // create null mvp view 189 | if (mNullViewProxy == null) { 190 | mNullViewProxy = createView(getMvpViewClass()); 191 | } 192 | view = mNullViewProxy; 193 | } 194 | return view; 195 | } 196 | 197 | 198 | /** 199 | * 创建 mvp view 200 | * @param viewClz 201 | * @param 202 | * @return 203 | */ 204 | public static T createView(Class viewClz) { 205 | return (T) Proxy.newProxyInstance(viewClz.getClassLoader(), 206 | new Class[] { viewClz }, NULL_VIEW); 207 | } 208 | 209 | 210 | private Class getMvpViewClass() { 211 | if (mMvpViewClass == null) { 212 | Type genType = getClass().getGenericSuperclass(); 213 | Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); 214 | mMvpViewClass = (Class) params[0]; 215 | } 216 | return mMvpViewClass; 217 | } 218 | 219 | 220 | /** 221 | * 动态代理 InvocationHandler 222 | */ 223 | private static class MvpViewInvocationHandler implements InvocationHandler { 224 | 225 | @Override 226 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 227 | Log.e("", "### MvpView InvocationHandler do nothing -> " + method.getName()) ; 228 | return null; 229 | } 230 | } 231 | } 232 | 233 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/biz/OnRequestListener.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.biz; 2 | 3 | import java.util.List; 4 | 5 | /** 6 | * Created by Dante on 2016/3/16. 7 | */ 8 | public interface OnRequestListener { 9 | 10 | void onSuccess(List data); 11 | void onFailed(); 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/biz/RequestBiz.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.biz; 2 | 3 | 4 | public interface RequestBiz { 5 | 6 | void requestForData(OnRequestListener listener); 7 | } 8 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/biz/RequestBiziml.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.biz; 2 | 3 | import java.util.ArrayList; 4 | 5 | /** 6 | * Created by Dante on 2016/3/16. 7 | */ 8 | public class RequestBiziml implements RequestBiz{ 9 | 10 | @Override 11 | public void requestForData(final OnRequestListener listener) { 12 | 13 | new Thread(new Runnable() { 14 | @Override 15 | public void run() { 16 | try { 17 | Thread.sleep(2000); 18 | ArrayList data = new ArrayList(); 19 | for(int i = 1 ; i< 8 ; i++){ 20 | data.add("item"+i); 21 | } 22 | if(null != listener){ 23 | listener.onSuccess(data); 24 | } 25 | }catch(Exception e){ 26 | e.printStackTrace(); 27 | } 28 | } 29 | }).start(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/mvc/MVCActivity.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.mvc; 2 | 3 | import android.os.Bundle; 4 | import android.os.Handler; 5 | import android.os.Looper; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.view.View; 8 | import android.widget.AdapterView; 9 | import android.widget.ArrayAdapter; 10 | import android.widget.ListView; 11 | import android.widget.ProgressBar; 12 | import android.widget.Toast; 13 | 14 | import java.util.List; 15 | 16 | import haibuzou.mvpsample.R; 17 | import haibuzou.mvpsample.biz.OnRequestListener; 18 | import haibuzou.mvpsample.biz.RequestBiz; 19 | import haibuzou.mvpsample.biz.RequestBiziml; 20 | 21 | public class MVCActivity extends AppCompatActivity{ 22 | 23 | private ListView mvcListView; 24 | private RequestBiz requestBiz; 25 | private ProgressBar pb; 26 | private Handler handler; 27 | 28 | @Override 29 | protected void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_mvc); 32 | mvcListView = (ListView)findViewById(R.id.mvc_listview); 33 | pb = (ProgressBar) findViewById(R.id.mvc_loading); 34 | pb.setVisibility(View.VISIBLE); 35 | handler = new Handler(Looper.getMainLooper()); 36 | requestBiz = new RequestBiziml(); 37 | requestForData(); 38 | } 39 | 40 | public void requestForData(){ 41 | requestBiz.requestForData(new OnRequestListener() { 42 | @Override 43 | public void onSuccess(final List data) { 44 | handler.post(new Runnable() { 45 | @Override 46 | public void run() { 47 | pb.setVisibility(View.GONE); 48 | ArrayAdapter adapter = new ArrayAdapter(MVCActivity.this,android.R.layout.simple_list_item_1,data); 49 | mvcListView.setAdapter(adapter); 50 | mvcListView.setOnItemClickListener(itemClickListener); 51 | } 52 | }); 53 | 54 | } 55 | 56 | @Override 57 | public void onFailed() { 58 | pb.setVisibility(View.GONE); 59 | Toast.makeText(MVCActivity.this,"加载失败",Toast.LENGTH_SHORT).show(); 60 | } 61 | }); 62 | } 63 | 64 | private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() { 65 | @Override 66 | public void onItemClick(AdapterView parent, View view, int position, long id) { 67 | Toast.makeText(MVCActivity.this,"点击了item"+(position+1),Toast.LENGTH_SHORT).show(); 68 | } 69 | }; 70 | } 71 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/mvp/MVPActivity.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.mvp; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.view.View; 6 | import android.widget.AdapterView; 7 | import android.widget.ArrayAdapter; 8 | import android.widget.ListView; 9 | import android.widget.ProgressBar; 10 | import android.widget.Toast; 11 | 12 | import java.util.List; 13 | 14 | import haibuzou.mvpsample.R; 15 | import haibuzou.mvpsample.mvp.presenter.MvpPresenter; 16 | import haibuzou.mvpsample.mvp.view.MvpView; 17 | 18 | public class MVPActivity extends AppCompatActivity implements MvpView ,AdapterView.OnItemClickListener{ 19 | 20 | private ListView mvpListView; 21 | private MvpPresenter mvpPresenter; 22 | private ProgressBar pb; 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.activity_mvp); 28 | mvpListView = (ListView)findViewById(R.id.mvp_listview); 29 | mvpListView.setOnItemClickListener(this); 30 | pb = (ProgressBar) findViewById(R.id.mvp_loading); 31 | mvpPresenter = new MvpPresenter(this); 32 | } 33 | 34 | @Override 35 | protected void onResume() { 36 | super.onResume(); 37 | mvpPresenter.onResume(); 38 | } 39 | 40 | @Override 41 | public void onItemClick(AdapterView parent, View view, int position, long id) { 42 | mvpPresenter.onItemClick(position); 43 | } 44 | 45 | @Override 46 | public void showLoading() { 47 | pb.setVisibility(View.VISIBLE); 48 | } 49 | 50 | @Override 51 | public void hideLoading() { 52 | pb.setVisibility(View.GONE); 53 | } 54 | 55 | @Override 56 | public void setListItem(List data) { 57 | ArrayAdapter adapter = new ArrayAdapter(MVPActivity.this,android.R.layout.simple_list_item_1,data); 58 | mvpListView.setAdapter(adapter); 59 | } 60 | 61 | //退出时销毁持有Activity 62 | @Override 63 | protected void onDestroy() { 64 | mvpPresenter.onDestroy(); 65 | super.onDestroy(); 66 | } 67 | 68 | @Override 69 | public void showMessage(String message) { 70 | Toast.makeText(this,message,Toast.LENGTH_SHORT).show(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/mvp/presenter/MvpPresenter.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.mvp.presenter; 2 | 3 | 4 | import android.os.Handler; 5 | import android.os.Looper; 6 | 7 | import java.util.List; 8 | 9 | import haibuzou.mvpsample.biz.OnRequestListener; 10 | import haibuzou.mvpsample.biz.RequestBiz; 11 | import haibuzou.mvpsample.biz.RequestBiziml; 12 | import haibuzou.mvpsample.mvp.view.MvpView; 13 | 14 | public class MvpPresenter { 15 | 16 | private MvpView mvpView; 17 | private RequestBiz requestBiz; 18 | private Handler mHandler; 19 | 20 | public MvpPresenter(MvpView mvpView) { 21 | this.mvpView = mvpView; 22 | requestBiz = new RequestBiziml(); 23 | mHandler = new Handler(Looper.getMainLooper()); 24 | } 25 | 26 | public void onResume(){ 27 | mvpView.showLoading(); 28 | requestBiz.requestForData(new OnRequestListener() { 29 | @Override 30 | public void onSuccess(final List data) { 31 | mHandler.post(new Runnable() { 32 | @Override 33 | public void run() { 34 | mvpView.hideLoading(); 35 | mvpView.setListItem(data); 36 | } 37 | }); 38 | 39 | } 40 | 41 | @Override 42 | public void onFailed() { 43 | mvpView.showMessage("请求失败"); 44 | } 45 | }); 46 | } 47 | 48 | public void onDestroy(){ 49 | mvpView = null; 50 | } 51 | 52 | public void onItemClick(int position){ 53 | mvpView.showMessage("点击了item"+position); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/mvp/view/MvpView.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.mvp.view; 2 | 3 | 4 | import java.util.List; 5 | 6 | public interface MvpView { 7 | void showLoading(); 8 | void hideLoading(); 9 | void setListItem(List data); 10 | void showMessage(String message); 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/newmvp/NewMvpActivity.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.newmvp; 2 | 3 | import android.os.Bundle; 4 | import android.view.View; 5 | import android.widget.AdapterView; 6 | import android.widget.ArrayAdapter; 7 | import android.widget.ListView; 8 | import android.widget.ProgressBar; 9 | import android.widget.Toast; 10 | 11 | import java.util.List; 12 | 13 | import haibuzou.mvpsample.R; 14 | import haibuzou.mvpsample.basemvp.BaseMvpActivity; 15 | 16 | 17 | public class NewMvpActivity extends BaseMvpActivity implements NewMvpView,AdapterView.OnItemClickListener{ 18 | 19 | private ListView mvpListView; 20 | private ProgressBar pb; 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_mvp); 26 | mvpListView = (ListView)findViewById(R.id.mvp_listview); 27 | mvpListView.setOnItemClickListener(this); 28 | pb = (ProgressBar) findViewById(R.id.mvp_loading); 29 | } 30 | 31 | @Override 32 | protected void onResume() { 33 | super.onResume(); 34 | presenter.onResume(); 35 | } 36 | 37 | @Override 38 | public NewMvpPresenter initPresenter() { 39 | return new NewMvpPresenter(); 40 | } 41 | 42 | @Override 43 | public void onItemClick(AdapterView parent, View view, int position, long id) { 44 | presenter.onItemClick(position); 45 | } 46 | 47 | @Override 48 | public void setListItem(List data) { 49 | ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,data); 50 | mvpListView.setAdapter(adapter); 51 | } 52 | 53 | @Override 54 | public void showMessage(String message) { 55 | Toast.makeText(this,message,Toast.LENGTH_SHORT).show(); 56 | } 57 | 58 | @Override 59 | public void showLoading() { 60 | pb.setVisibility(View.VISIBLE); 61 | } 62 | 63 | @Override 64 | public void hideLoading() { 65 | pb.setVisibility(View.GONE); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/newmvp/NewMvpPresenter.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.newmvp; 2 | 3 | 4 | import android.os.Handler; 5 | import android.os.Looper; 6 | 7 | import java.util.List; 8 | 9 | import haibuzou.mvpsample.basemvp.BasePresenter; 10 | import haibuzou.mvpsample.biz.OnRequestListener; 11 | import haibuzou.mvpsample.biz.RequestBiz; 12 | import haibuzou.mvpsample.biz.RequestBiziml; 13 | 14 | public class NewMvpPresenter extends BasePresenter { 15 | 16 | private RequestBiz requestBiz; 17 | private Handler mHandler; 18 | 19 | public NewMvpPresenter() { 20 | requestBiz = new RequestBiziml(); 21 | mHandler = new Handler(Looper.getMainLooper()); 22 | } 23 | 24 | public void onResume(){ 25 | requestBiz.requestForData(new OnRequestListener() { 26 | 27 | @Override 28 | public void onSuccess(final List data) { 29 | mHandler.post(new Runnable() { 30 | @Override 31 | public void run() { 32 | mView.hideLoading(); 33 | mView.setListItem(data); 34 | } 35 | }); 36 | } 37 | 38 | @Override 39 | public void onFailed() { 40 | mView.showMessage("请求失败"); 41 | } 42 | }); 43 | } 44 | 45 | public void onItemClick(int position){ 46 | mView.showMessage("点击了item"+position); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/haibuzou/mvpsample/newmvp/NewMvpView.java: -------------------------------------------------------------------------------- 1 | package haibuzou.mvpsample.newmvp; 2 | 3 | import java.util.List; 4 | 5 | import haibuzou.mvpsample.basemvp.BaseView; 6 | 7 | 8 | public interface NewMvpView extends BaseView { 9 | void setListItem(List data); 10 | void showMessage(String message); 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 |