MVP模式的几种构建方式

前言

主要介绍MVP模式的几种构建方法,大多数都是自己看。咳咳,所以不太会整理这部分内容;

一:一般构建

BaseActivity.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
public abstract class BaseActivity<V, T extends BasePresenter<V>> extends AppCompatActivity {

protected T mPresenter;
private CustomDialog mDialogWaiting;
private MaterialDialog mMaterialDialog;

//以下是所有Activity中可能会出现的控件
@Bind(R.id.appBar)
protected AppBarLayout mAppBar;
// @Bind(R.id.toolbar)
// protected Toolbar mToolbar;
@Bind(R.id.flToolbar)
public FrameLayout mToolbar;
@Bind(R.id.ivToolbarNavigation)
public ImageView mToolbarNavigation;
@Bind(R.id.vToolbarDivision)
public View mToolbarDivision;
@Bind(R.id.llToolbarTitle)
public AutoLinearLayout mLlToolbarTitle;
@Bind(R.id.tvToolbarTitle)
public TextView mToolbarTitle;
@Bind(R.id.tvToolbarSubTitle)
public TextView mToolbarSubTitle;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApp.activities.add(this);
init();

//判断是否使用MVP模式
mPresenter = createPresenter();
if (mPresenter != null) {
mPresenter.attachView((V) this);//因为之后所有的子类都要实现对应的View接口
}

//子类不再需要设置布局ID,也不再需要使用ButterKnife.bind()
setContentView(provideContentViewId());
ButterKnife.bind(this);

setupAppBarAndToolbar();

//沉浸式状态栏
StatusBarUtil.setColor(this, UIUtils.getColor(R.color.colorPrimaryDark), 10);

initView();
initData();
initListener();
}

/**
* 设置AppBar和Toolbar
*/
private void setupAppBarAndToolbar() {
//如果该应用运行在android 5.0以上设备,设置标题栏的z轴高度
if (mAppBar != null && Build.VERSION.SDK_INT > 21) {
mAppBar.setElevation(10.6f);
}

//如果界面中有使用toolbar,则使用toolbar替代actionbar
//默认不是使用NoActionBar主题,所以如果需要使用Toolbar,需要自定义NoActionBar主题后,在AndroidManifest.xml中对指定Activity设置theme
// if (mToolbar != null) {
// setSupportActionBar(mToolbar);
// if (isToolbarCanBack()) {
// ActionBar actionBar = getSupportActionBar();
// if (actionBar != null) {
// actionBar.setDisplayHomeAsUpEnabled(true);
// }
// }
// }

mToolbarNavigation.setVisibility(isToolbarCanBack() ? View.VISIBLE : View.GONE);
mToolbarDivision.setVisibility(isToolbarCanBack() ? View.VISIBLE : View.GONE);
mToolbarNavigation.setOnClickListener(v -> onBackPressed());
mLlToolbarTitle.setPadding(isToolbarCanBack() ? 0 : 40, 0, 0, 0);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onDestroy() {
super.onDestroy();
if (mPresenter != null) {
mPresenter.detachView();
}
}

//在setContentView()调用之前调用,可以设置WindowFeature(如:this.requestWindowFeature(Window.FEATURE_NO_TITLE);)
public void init() {
}

public void initView() {
}

public void initData() {
}

public void initListener() {
}

//用于创建Presenter和判断是否使用MVP模式(由子类实现)
protected abstract T createPresenter();

//得到当前界面的布局文件id(由子类实现)
protected abstract int provideContentViewId();

/**
* 是否让Toolbar有返回按钮(默认可以,一般一个应用中除了主界面,其他界面都是可以有返回按钮的)
*/
protected boolean isToolbarCanBack() {
return true;
}

/**
* 显示等待提示框
*/
public Dialog showWaitingDialog(String tip) {
hideWaitingDialog();
View view = View.inflate(this, R.layout.dialog_waiting, null);
if (!TextUtils.isEmpty(tip))
((TextView) view.findViewById(R.id.tvTip)).setText(tip);
mDialogWaiting = new CustomDialog(this, view, R.style.MyDialog);
mDialogWaiting.show();
mDialogWaiting.setCancelable(false);
return mDialogWaiting;
}

/**
* 隐藏等待提示框
*/
public void hideWaitingDialog() {
if (mDialogWaiting != null) {
mDialogWaiting.dismiss();
mDialogWaiting = null;
}
}

/**
* 显示MaterialDialog
*/
public MaterialDialog showMaterialDialog(String title, String message, String positiveText, String negativeText, View.OnClickListener positiveButtonClickListener, View.OnClickListener negativeButtonClickListener) {
hideMaterialDialog();
mMaterialDialog = new MaterialDialog(this);
if (!TextUtils.isEmpty(title)) {
mMaterialDialog.setTitle(title);
}
if (!TextUtils.isEmpty(message)) {
mMaterialDialog.setMessage(message);
}
if (!TextUtils.isEmpty(positiveText)) {
mMaterialDialog.setPositiveButton(positiveText, positiveButtonClickListener);
}
if (!TextUtils.isEmpty(negativeText)) {
mMaterialDialog.setNegativeButton(negativeText, negativeButtonClickListener);
}
mMaterialDialog.show();
return mMaterialDialog;
}

/**
* 隐藏MaterialDialog
*/
public void hideMaterialDialog() {
if (mMaterialDialog != null) {
mMaterialDialog.dismiss();
mMaterialDialog = null;
}
}

public void jumpToActivity(Intent intent) {
startActivity(intent);
}

public void jumpToActivity(Class activity) {
Intent intent = new Intent(this, activity);
startActivity(intent);
}

public void jumpToWebViewActivity(String url) {
Intent intent = new Intent(this, WebViewActivity.class);
intent.putExtra("url", url);
jumpToActivity(intent);
}


public void jumpToActivityAndClearTask(Class activity) {
Intent intent = new Intent(this, activity);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}

public void jumpToActivityAndClearTop(Class activity) {
Intent intent = new Intent(this, activity);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}

/*------------------ toolbar的一些视图操作 ------------------*/
public void setToolbarTitle(String title) {
mToolbarTitle.setText(title);
}

public void setToolbarSubTitle(String subTitle) {
mToolbarSubTitle.setText(subTitle);
mToolbarSubTitle.setVisibility(subTitle.length() > 0 ? View.VISIBLE : View.GONE);
}

}

AddFriendActivity.class

具体的Activity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class AddFriendActivity extends BaseActivity<IAddFriendAtView, AddFriendAtPresenter> implements IAddFriendAtView {

@Bind(R.id.llSearchUser)
LinearLayout mLlSearchUser;
@Bind(R.id.tvAccount)
TextView mTvAccount;

@Override
public void initView() {
setToolbarTitle(UIUtils.getString(R.string.add_friend));
mTvAccount.setText(UserCache.getId() + "");
}

@Override
public void initListener() {
mLlSearchUser.setOnClickListener(v -> jumpToActivity(SearchUserActivity.class));
}

@Override
protected AddFriendAtPresenter createPresenter() {
return new AddFriendAtPresenter(this);
}

@Override
protected int provideContentViewId() {
return R.layout.activity_add_friend;
}
}

BasePresenter.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class BasePresenter<V> {

/*================== 以下是网络请求接口 ==================*/

public BaseActivity mContext;

public BasePresenter(BaseActivity context) {
mContext = context;
}

protected Reference<V> mViewRef;

public void attachView(V view) {
mViewRef = new WeakReference<V>(view);
}

public boolean isViewAttached() {
return mViewRef != null && mViewRef.get() != null;
}

public void detachView() {
if (mViewRef != null) {
mViewRef.clear();
mViewRef = null;
}
}

public V getView() {
return mViewRef != null ? mViewRef.get() : null;
}

}

AddFriendAtPresenter.class

具体的Presenter

1
2
3
4
5
6
public class AddFriendAtPresenter extends BasePresenter<IAddFriendAtView> {

public AddFriendAtPresenter(BaseActivity context) {
super(context);
}
}

IAddFriendAtView.class

接口根据情况具体实现

1
2
public interface IAddFriendAtView {
}

BaseFragment.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public abstract class BaseFragment<V, T extends BasePresenter<V>> extends Fragment {

protected T mPresenter;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

init();

//判断是否使用MVP模式
mPresenter = createPresenter();
if (mPresenter != null) {
mPresenter.attachView((V) this);//因为之后所有的子类都要实现对应的View接口
}
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//子类不再需要设置布局ID,也不再需要使用ButterKnife.bind()
View rootView = inflater.inflate(provideContentViewId(), container, false);
ButterKnife.bind(this, rootView);
initView(rootView);
return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
initListener();
}

@Override
public void onDestroy() {
super.onDestroy();
if (mPresenter != null) {
mPresenter.detachView();
}
}

public void init() {

}

public void initView(View rootView) {
}

public void initData() {

}

public void initListener() {

}

//用于创建Presenter和判断是否使用MVP模式(由子类实现)
protected abstract T createPresenter();

//得到当前界面的布局文件id(由子类实现)
protected abstract int provideContentViewId();
}

实用类

JsonMananger.class

FastJson的封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @描述 json管理器(fastjson)
*/
public class JsonMananger {

static {
TypeUtils.compatibleWithJavaBean = true;
}

private static final String tag = JsonMananger.class.getSimpleName();

/**
* 将json字符串转换成java对象
*
* @param json
* @param cls
* @return
* @throws HttpException
*/
public static <T> T jsonToBean(String json, Class<T> cls) throws HttpException {
return JSON.parseObject(json, cls);
}

/**
* 将json字符串转换成java List对象
*
* @param json
* @param cls
* @return
* @throws HttpException
*/
public static <T> List<T> jsonToList(String json, Class<T> cls) throws HttpException {
return JSON.parseArray(json, cls);
}

/**
* 将bean对象转化成json字符串
*
* @param obj
* @return
* @throws HttpException
*/
public static String beanToJson(Object obj) throws HttpException {
String result = JSON.toJSONString(obj);
Log.e(tag, "beanToJson: " + result);
return result;
}

}
戴定康 wechat
欢迎您扫一扫上面的微信,加我为好友!