-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBaseHttpRecyclerActivity.java
executable file
·255 lines (175 loc) · 6.89 KB
/
BaseHttpRecyclerActivity.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
/*Copyright ©2015 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 zuo.biao.library.base;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import java.util.List;
import zuo.biao.library.R;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.interfaces.OnLoadListener;
import zuo.biao.library.interfaces.OnStopLoadListener;
import zuo.biao.library.util.Log;
/**基础http网络列表的Activity
* @author Lemon
* @param <T> 数据模型(model/JavaBean)类
* @param <VH> ViewHolder或其子类
* @param <A> 管理LV的Adapter
* @see #getListAsync(int)
* @see #onHttpResponse(int, String, Exception)
* @see
* <pre>
* 基础使用:<br />
* extends BaseHttpRecyclerActivity 并在子类onCreate中srlBaseHttpRecycler.autoRefresh(), 具体参考.DemoHttpRecyclerActivity
* <br /><br />
* 列表数据加载及显示过程:<br />
* 1.srlBaseHttpRecycler.autoRefresh触发刷新 <br />
* 2.getListAsync异步获取列表数据 <br />
* 3.onHttpResponse处理获取数据的结果 <br />
* 4.setList把列表数据绑定到adapter <br />
* </pre>
*/
public abstract class BaseHttpRecyclerActivity<T, VH extends RecyclerView.ViewHolder, A extends RecyclerView.Adapter<VH>>
extends BaseRecyclerActivity<T, VH, A>
implements OnHttpResponseListener, OnStopLoadListener, OnRefreshListener, OnLoadmoreListener {
private static final String TAG = "BaseHttpRecyclerActivity";
//UI显示区(操作UI,但不存在数据获取或处理代码,也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
protected SmartRefreshLayout srlBaseHttpRecycler;
@Override
public void initView() {
super.initView();
srlBaseHttpRecycler = findView(R.id.srlBaseHttpRecycler);
}
@Override
public void setAdapter(A adapter) {
if (adapter instanceof BaseAdapter) {
((zuo.biao.library.base.BaseAdapter) adapter).setOnLoadListener(new OnLoadListener() {
@Override
public void onRefresh() {
srlBaseHttpRecycler.autoRefresh();
}
@Override
public void onLoadMore() {
srlBaseHttpRecycler.autoLoadmore();
}
});
}
super.setAdapter(adapter);
}
//UI显示区(操作UI,但不存在数据获取或处理代码,也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//Data数据区(存在数据获取或处理代码,但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@Override
public void initData() {
super.initData();
}
/**
* @param page 用-page作为requestCode
*/
@Override
public abstract void getListAsync(int page);
/**
* 将JSON串转为List(已在非UI线程中)
* *直接JSON.parseArray(json, getCacheClass());可以省去这个方法,但由于可能json不完全符合parseArray条件,所以还是要保留。
* *比如json只有其中一部分能作为parseArray的字符串时,必须先提取出这段字符串再parseArray
*/
public abstract List<T> parseArray(String json);
//Data数据区(存在数据获取或处理代码,但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@Override
public void initEvent() {//必须调用
super.initEvent();
setOnStopLoadListener(this);
srlBaseHttpRecycler.setOnRefreshListener(this);
srlBaseHttpRecycler.setOnLoadmoreListener(this);
}
/**重写后可自定义对这个事件的处理
* @param parent
* @param view
* @param position
* @param id
*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
@Override
public void onRefresh(RefreshLayout refreshlayout) {
onRefresh();
}
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
onLoadMore();
}
@Override
public void onStopRefresh() {
runUiThread(new Runnable() {
@Override
public void run() {
srlBaseHttpRecycler.finishRefresh();
srlBaseHttpRecycler.setLoadmoreFinished(false);
}
});
}
@Override
public void onStopLoadMore(final boolean isHaveMore) {
runUiThread(new Runnable() {
@Override
public void run() {
if (isHaveMore) {
srlBaseHttpRecycler.finishLoadmore();
} else {
srlBaseHttpRecycler.finishLoadmoreWithNoMoreData();
}
srlBaseHttpRecycler.setLoadmoreFinished(! isHaveMore);
}
});
}
/**处理Http请求结果
* @param requestCode = -page {@link #getListAsync(int)}
* @param resultJson
* @param e
*/
@Override
public void onHttpResponse(final int requestCode, final String resultJson, final Exception e) {
runThread(TAG + "onHttpResponse", new Runnable() {
@Override
public void run() {
int page = 0;
if (requestCode > 0) {
Log.w(TAG, "requestCode > 0, 应该用BaseListFragment#getListAsync(int page)中的page的负数作为requestCode!");
} else {
page = - requestCode;
}
onResponse(page, parseArray(resultJson), e);
}
});
}
/**处理结果
* @param page
* @param list
* @param e
*/
public void onResponse(int page, List<T> list, Exception e) {
if ((list == null || list.isEmpty()) && e != null) {
onLoadFailed(page, e);
} else {
onLoadSucceed(page, list);
}
}
//生命周期、onActivityResult<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//生命周期、onActivityResult>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//Event事件区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//内部类,尽量少用<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//内部类,尽量少用>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
}