14 | * 相同类型的实例只维护一个,M 和 P的实例第一次创建后会一直复用,V 的实例会更新到最近使用的实例
15 | */
16 | public class Mvp pClass) {
96 | initMap();
97 |
98 | P pInstance = null;
99 |
100 | if (!sInstanceMap.containsKey(pClass.getName())) {
101 | try {
102 |
103 | pInstance = (P) Class.forName(pClass.getName()).newInstance();
104 | sInstanceMap.put(pClass.getName(), pInstance);
105 |
106 | } catch (InstantiationException e) {
107 | e.printStackTrace();
108 | } catch (IllegalAccessException e) {
109 | e.printStackTrace();
110 | } catch (ClassNotFoundException e) {
111 | e.printStackTrace();
112 | }
113 | }
114 | }
115 |
116 | /**
117 | * 获取Model层实例, 为null的话, 走创建流程
118 | *
119 | * @param mClass
120 | * @return
121 | */
122 | public M getModel(Class pClass) {
155 | initMap();
156 |
157 | if (!sInstanceMap.containsKey(pClass.getName())) {
158 | registerPresenter(pClass);
159 | }
160 |
161 | return (P) sInstanceMap.get(pClass.getName());
162 | }
163 |
164 | /**
165 | * 移除实例
166 | */
167 | public void unRegister(Class clazz) {
168 | if (sInstanceMap == null) {
169 | return;
170 | }
171 |
172 | sInstanceMap.remove(clazz.getName());
173 | }
174 |
175 |
176 | /**
177 | * 获取父类泛型的类型
178 | *
179 | * @param o
180 | * @param i
181 | * @return
182 | */
183 | public static Class getGenericType(Object o, int i) {
184 | Type type = o.getClass().getGenericSuperclass();
185 | ParameterizedType p = (ParameterizedType) type;
186 | Class c = (Class) p.getActualTypeArguments()[i];
187 | return c;
188 | }
189 | }
190 |
--------------------------------------------------------------------------------
/mvpframelib/src/main/java/com/wolearn/mvpframelib/frame/MvpActivity.java:
--------------------------------------------------------------------------------
1 | package com.wolearn.mvpframelib.frame;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v7.app.AppCompatActivity;
6 |
7 | import com.wolearn.mvpframelib.base.BaseView;
8 |
9 | /**
10 | * Created by wulei
11 | * Data: 2016/8/4.
12 | */
13 | public abstract class MvpActivity extends AppCompatActivity implements MvpView{
14 | public P mPresenter;
15 |
16 | @Override
17 | protected void onCreate(@Nullable Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 |
20 | initMvp();
21 | }
22 |
23 | public void initMvp(){
24 | Mvp mvp = Mvp.getInstance();
25 | mvp.registerView(this.getClass(), this);
26 | mPresenter = (P) mvp.getPresenter(Mvp.getGenericType(this, 0));
27 | mPresenter.initPresenter(getBaseView());
28 | }
29 |
30 | /**
31 | * 确定IView类型
32 | * @return
33 | */
34 | public abstract BaseView getBaseView();
35 |
36 | @Override
37 | protected void onDestroy() {
38 | super.onDestroy();
39 |
40 | Mvp.getInstance().unRegister(this.getClass());
41 | mPresenter.destory();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/mvpframelib/src/main/java/com/wolearn/mvpframelib/frame/MvpFragment.java:
--------------------------------------------------------------------------------
1 | package com.wolearn.mvpframelib.frame;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.support.v4.app.Fragment;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 |
10 | import com.wolearn.mvpframelib.base.BaseView;
11 |
12 | /**
13 | * Created by wulei
14 | * Data: 2016/8/4.
15 | */
16 | public abstract class MvpFragment extends Fragment implements MvpView{
17 | public P mPresenter;
18 |
19 | @Nullable
20 | @Override
21 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
22 | initMvp();
23 | return super.onCreateView(inflater, container, savedInstanceState);
24 | }
25 |
26 | public void initMvp(){
27 | Mvp mvp = Mvp.getInstance();
28 | mvp.registerView(this.getClass(), this);
29 | mPresenter = (P) mvp.getPresenter(Mvp.getGenericType(this, 0));
30 | mPresenter.initPresenter(getBaseView());
31 | }
32 |
33 | public abstract BaseView getBaseView();
34 |
35 | @Override
36 | public void onDestroy() {
37 | super.onDestroy();
38 |
39 | Mvp.getInstance().unRegister(this.getClass());
40 | mPresenter.destory();
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/mvpframelib/src/main/java/com/wolearn/mvpframelib/frame/MvpModel.java:
--------------------------------------------------------------------------------
1 | package com.wolearn.mvpframelib.frame;
2 |
3 | /**
4 | * Created by wulei
5 | * Data: 2016/8/3.
6 | */
7 | public class MvpModel {
8 |
9 | public void execute() {
10 |
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/mvpframelib/src/main/java/com/wolearn/mvpframelib/frame/MvpPresenter.java:
--------------------------------------------------------------------------------
1 | package com.wolearn.mvpframelib.frame;
2 |
3 | import android.content.Context;
4 |
5 | import com.wolearn.mvpframelib.base.BaseView;
6 |
7 | import java.lang.ref.Reference;
8 | import java.lang.ref.WeakReference;
9 |
10 |
11 | /**
12 | * Created by wulei
13 | * Data: 2016/8/4.
14 | */
15 | public class MvpPresenter