-
Notifications
You must be signed in to change notification settings - Fork 7
/
MainTabActivity.java
executable file
·363 lines (267 loc) · 10.9 KB
/
MainTabActivity.java
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package apijson.demo.activity_fragment;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import apijson.demo.base.BaseBottomTabActivity;
import apijson.demo.R;
import apijson.demo.application.DemoApplication;
import apijson.demo.interfaces.TopBarMenuCallback;
import apijson.demo.model.User;
import apijson.demo.util.ActionUtil;
import apijson.demo.util.HttpRequest;
import apijson.demo.util.TestRequestAndResponseJava;
//import apijson.demo.util.TestRequestAndResponseKt;
import uiauto.UIAutoActivity;
import zuo.biao.apijson.JSON;
import zuo.biao.apijson.JSONResponse;
import zuo.biao.library.base.BaseBroadcastReceiver;
import zuo.biao.library.interfaces.OnBottomDragListener;
import zuo.biao.library.manager.HttpManager.OnHttpResponseListener;
import zuo.biao.library.ui.EditTextInfoWindow;
import zuo.biao.library.util.Log;
import zuo.biao.library.util.StringUtil;
/**应用主页
* @author Lemon
* @use MainTabActivity.createIntent(...)
*/
public class MainTabActivity extends BaseBottomTabActivity implements OnBottomDragListener {
private static final String TAG = "MainTabActivity";
//启动方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
/**启动这个Activity的Intent
* @param context
* @return
*/
public static Intent createIntent(Context context) {
return new Intent(context, MainTabActivity.class);
}
//启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
@Override
public Activity getActivity() {
return this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_tab_activity, this);
//功能归类分区方法,必须调用<<<<<<<<<<
initView();
initData();
initEvent();
//功能归类分区方法,必须调用>>>>>>>>>>
findViewById(R.id.tvBaseTitle, new View.OnClickListener() {
@Override
public void onClick(View v) {
toActivity(UIAutoActivity.createIntent(context));
}
});
BaseBroadcastReceiver.register(context, receiver
, new String[]{ACTION_EXIT_APP, ActionUtil.ACTION_RELOAD_CURRENT_USER});
// if (SettingUtil.isOnTestMode) {
showShortToast("测试服务器\n" + HttpRequest.URL_BASE);
HttpRequest.get(JSON.parseObject(JSON.toJSONString(TestRequestAndResponseJava.request()), zuo.biao.apijson.JSONObject.class), 0, new OnHttpResponseListener() {
@Override
public void onHttpResponse(int requestCode, String resultJson, Exception e) {
TestRequestAndResponseJava.response(resultJson);
if (e != null) {
e.printStackTrace();
}
}
});
HttpRequest.get(JSON.parseObject(JSON.toJSONString(TestRequestAndResponseJava.smartRequest()), zuo.biao.apijson.JSONObject.class), 0, new OnHttpResponseListener() {
@Override
public void onHttpResponse(int requestCode, String resultJson, Exception e) {
TestRequestAndResponseJava.smartResponse(resultJson);
if (e != null) {
e.printStackTrace();
}
}
});
// HttpRequest.get(JSON.parseObject(JSON.toJSONString(TestRequestAndResponseKt.request()), zuo.biao.apijson.JSONObject.class), 0, new OnHttpResponseListener() {
// @Override
// public void onHttpResponse(int requestCode, String resultJson, Exception e) {
// TestRequestAndResponseKt.response(resultJson);
// if (e != null) {
// e.printStackTrace();
// }
// }
// });
//
// HttpRequest.get(JSON.parseObject(JSON.toJSONString(TestRequestAndResponseKt.smartRequest()), zuo.biao.apijson.JSONObject.class), 0, new OnHttpResponseListener() {
// @Override
// public void onHttpResponse(int requestCode, String resultJson, Exception e) {
// TestRequestAndResponseKt.smartResponse(resultJson);
// if (e != null) {
// e.printStackTrace();
// }
// }
// });
// }
}
// UI显示区(操作UI,但不存在数据获取或处理代码,也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
private ViewGroup llMainTabLeftContainer;
private ViewGroup llMainTabRightContainer;
@Override
public void initView() {// 必须调用
super.initView();
exitAnim = R.anim.bottom_push_out;
llMainTabLeftContainer = (ViewGroup) findViewById(R.id.llMainTabLeftContainer);
llMainTabRightContainer = (ViewGroup) findViewById(R.id.llMainTabRightContainer);
}
@Override
protected int[] getTabClickIds() {
return new int[]{R.id.llMainTabTab0, R.id.llMainTabTab1, R.id.llMainTabTab2};
}
@Override
protected int[][] getTabSelectIds() {
return new int[][]{
new int[]{R.id.ivMainTabTab0, R.id.ivMainTabTab1, R.id.ivMainTabTab2},//顶部图标
new int[]{R.id.tvMainTabTab0, R.id.tvMainTabTab1, R.id.tvMainTabTab2}//底部文字
};
}
@Override
public int getFragmentContainerResId() {
return R.id.flMainTabFragmentContainer;
}
@Override
protected Fragment getFragment(int position) {
switch (position) {
case 1:
UserListFragment fragment = UserListFragment.createInstance();
fragment.setSearchType(EditTextInfoWindow.TYPE_NAME);
return fragment;
case 2:
return MineFragment.createInstance();
default:
return MomentListFragment.createInstance();
}
}
private static final String[] TAB_NAMES = {"圈子", "朋友", "我的"};
@Override
protected void selectTab(int position) {
tvBaseTitle.setText(TAB_NAMES[position]);
View left = null, right = null;
if (fragments[position] instanceof TopBarMenuCallback) {
left = ((TopBarMenuCallback) fragments[position]).getLeftMenu(context);
right = ((TopBarMenuCallback) fragments[position]).getRightMenu(context);
}
llMainTabLeftContainer.removeAllViews();
if (left != null) {
llMainTabLeftContainer.addView(left);
}
llMainTabRightContainer.removeAllViews();
if (right != null) {
llMainTabRightContainer.addView(right);
}
verifyLogin();
}
// UI显示区(操作UI,但不存在数据获取或处理代码,也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Data数据区(存在数据获取或处理代码,但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@Override
public void initData() {// 必须调用
super.initData();
if (isCurrentUserCorrect() == false) {
reloadAll();
}
}
@Override
protected void reloadAll() {
Log.d(TAG, "reloadAll >>> ");
HttpRequest.getUser(DemoApplication.getInstance().getCurrentUserId(), 0, new OnHttpResponseListener() {
@Override
public void onHttpResponse(int requestCode, String resultJson, Exception e) {
Log.d(TAG, "reloadAll >>> HttpRequest.getUser.onHttpResponse >> saveCurrentUser >>");
DemoApplication.getInstance().saveCurrentUser(new JSONResponse(resultJson).getObject(User.class));
runUiThread(new Runnable() {
@Override
public void run() {
sendBroadcast(new Intent(ActionUtil.ACTION_USER_CHANGED)
.putExtra(INTENT_ID, DemoApplication.getInstance().getCurrentUserId())
.putExtra(ActionUtil.INTENT_USER, DemoApplication.getInstance().getCurrentUser()));
}
});
}
});
}
// Data数据区(存在数据获取或处理代码,但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@Override
public void initEvent() {// 必须调用
super.initEvent();
}
@Override
public void onDragBottom(boolean rightToLeft) {
if (fragments[currentPosition] instanceof OnBottomDragListener) {
((OnBottomDragListener) fragments[currentPosition]).onDragBottom(rightToLeft);
}
}
// 系统自带监听方法<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//双击手机返回键退出<<<<<<<<<<<<<<<<<<<<<
private long firstTime = 0;//第一次返回按钮计时
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
long secondTime = System.currentTimeMillis();
if(secondTime - firstTime > 2000){
showShortToast("再按一次退出");
firstTime = secondTime;
} else {//完全退出
sendBroadcast(new Intent(ACTION_EXIT_APP));
}
return true;
}
return super.onKeyUp(keyCode, event);
}
//双击手机返回键退出>>>>>>>>>>>>>>>>>>>>>
// 类相关监听<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
private boolean isToExitApp = false;
@Override
protected void onDestroy() {
BaseBroadcastReceiver.unregister(context, receiver);
super.onDestroy();
if (isToExitApp) {
isToExitApp = false;
moveTaskToBack(true);//应用退到后台
System.exit(0);
}
}
// 类相关监听>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// 系统自带监听方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// 内部类,尽量少用<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent == null ? null : intent.getAction();
if (isAlive() == false || StringUtil.isNotEmpty(action, true) == false) {
Log.e(TAG, "receiver.onReceive isAlive() == false" +
" || StringUtil.isNotEmpty(action, true) == false >> return;");
return;
}
if (ACTION_EXIT_APP.equals(action)) {
isToExitApp = true;
finish();
return;
}
if (ActionUtil.ACTION_RELOAD_CURRENT_USER.equals(action)) {
reloadAll();// fragmentManager show remove等都会崩溃
}
}
};
// 内部类,尽量少用>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}