BaseLoadActivity.java
9.2 KB
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
package com.cnlive.goldenline.ui.base;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.ui.widget.CNAccelerometerSensor;
import com.cnlive.goldenline.ui.widget.pull2refresh.PullToRefreshLayout;
import com.cnlive.goldenline.util.LogUtils;
import com.umeng.analytics.MobclickAgent;
import com.umeng.message.PushAgent;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;
/**
* Created by zuoxian on 15/9/7.
*/
public abstract class BaseLoadActivity<T> extends AppCompatActivity implements Callback<T>, View.OnClickListener,
PullToRefreshLayout.OnPullListener {
@Nullable
@BindView(R.id.toolbar)
protected Toolbar toobar;
@Nullable
@BindView(R.id.img_logo)
protected ImageView img_logo;
@Nullable
@BindView(R.id.toolbar_title)
public TextView tv_title;
@Nullable
@BindView(R.id.btn_go_back)
public ImageButton imgBtn_back;
@Nullable
@BindView(R.id.movie_icon)
public ImageView movie_icon;
@Nullable
@BindView(R.id.menu_edit)
public TextView tv_edit;
@Nullable
@BindView(R.id.movie_icon_layout)
public MenuItem search_item;
@Nullable
@BindView(R.id.empty)
protected View mEmptyView;
@Nullable
@BindView(R.id.empty_load)
LinearLayout mEmptyProgressbar;
@Nullable
@BindView(R.id.empty_button)
Button mEmptyButton;
@Nullable
@BindView(R.id.pull_to_refresh)
protected PullToRefreshLayout mPullToRefreshLayout;
public boolean mIsOnPause;
protected T mPageData;
protected boolean isLoading;
protected boolean mIsPullToRefresh = false;
protected boolean enableLoadMore = false;
// //弹幕相关
// @Nullable
// @BindView(R.id.danmuview)
// public DanmakuView danmuview;
// //播放器上的控制器
// @Nullable
// @BindView(R.id.playerControllerView)
// public PlayerControllerView playerControllerView;
//
// protected DanmuUtil danmuUtil;
protected CNAccelerometerSensor cnAccelerometerSensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PushAgent.getInstance(this).onAppStart();
cnAccelerometerSensor = new CNAccelerometerSensor(this);
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
ButterKnife.bind(this);
initView();
// danmuUtil = new DanmuUtil(this, danmuview, playerControllerView);
}
/**
* 控件布局初始化
*/
protected void initView() {
if (toobar == null) {
toobar = (Toolbar) findViewById(R.id.toolbar);
}
if (toobar != null) {
setSupportActionBar(toobar);
}
if (tv_title == null) {
tv_title = (TextView) findViewById(R.id.toolbar_title);
}
if (imgBtn_back == null) {
imgBtn_back = (ImageButton) findViewById(R.id.btn_go_back);
}
if (imgBtn_back != null) imgBtn_back.setOnClickListener(this);
}
public void setMovieIcon(@DrawableRes int drawableId) {
if (movie_icon == null) {
movie_icon = (ImageView) findViewById(R.id.movie_icon);
}
movie_icon.setBackgroundResource(drawableId);
}
public void setTitle(String title) {
if (tv_title != null) {
tv_title.setText(title);
}
}
protected void hideTitle(boolean isVisiable) {
if (tv_title != null) {
tv_title.setVisibility(isVisiable ? View.VISIBLE : View.GONE);
}
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_go_back) {
finish();
} else if (v.getId() == R.id.empty_button) {
mIsPullToRefresh = false;
showLoad();
onLoadData();
}
}
public void onResume() {
super.onResume();
mIsOnPause = false;
MobclickAgent.onResume(this);
if (cnAccelerometerSensor != null) cnAccelerometerSensor.register();
// if (mPullToRefreshLayout != null) {
// if (!enableLoadMore) {
// LayoutInflater inflater = (LayoutInflater) this.getSystemService(
// Context.LAYOUT_INFLATER_SERVICE);
// mPullToRefreshLayout.setCustomLoadmoreView(inflater.inflate(R.layout.empty_view, null));
// }
// mPullToRefreshLayout.setOnPullListener(new PullToRefreshLayout.OnPullListener() {
// @Override
// public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
// mIsPullToRefresh = true;
// onLoadData();
// }
//
// @Override
// public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
// mPullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
// onLoadData();
// }
// });
// }
}
public void onPause() {
super.onPause();
mIsOnPause = true;
MobclickAgent.onResume(this);
if (cnAccelerometerSensor != null) cnAccelerometerSensor.unregister();
}
@Override
protected void onStart() {
super.onStart();
onLoadData();
}
/**
* 请求接口数据 下拉刷新 请求第一页数据
*/
protected abstract void onLoadData();
/**
* 加载更多 分页请求下一页
*/
protected abstract void onLoadNextPageData();
/**
* @param pageData
*/
protected abstract void onInitLoadData(T pageData);
@Override
public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
mIsPullToRefresh = true;
pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
onLoadData();
}
@Override
public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
onLoadNextPageData();
}
@Override
public void success(T t, Response response) {
onRefreshCallback(true);
}
@Override
public void failure(RetrofitError error) {
onRefreshCallback(false);
showConnectionRetry();
}
protected void setPageData(T page_data) {
this.mPageData = page_data;
onInitLoadData(page_data);
}
protected void showLoad() {
if (this == null) return;
if (mPageData != null) return;
if (mEmptyView != null) mEmptyView.setVisibility(View.VISIBLE);
if (mEmptyProgressbar != null) mEmptyProgressbar.setVisibility(View.VISIBLE);
if (mEmptyButton != null) mEmptyButton.setVisibility(View.GONE);
}
protected void showConnectionRetry() {
if (this == null) return;
if (mPageData != null) return;
if (mEmptyView != null) mEmptyView.setVisibility(View.VISIBLE);
if (mEmptyProgressbar != null) mEmptyProgressbar.setVisibility(View.GONE);
if (mEmptyButton != null) {
mEmptyButton.setText(getString(R.string.load_retry));
mEmptyButton.setVisibility(View.VISIBLE);
mEmptyButton.setOnClickListener(this);
}
}
protected void hideEmptyView() {
if (mEmptyView != null) mEmptyView.setVisibility(View.GONE);
}
// @NonNull
// protected View.OnClickListener mOnClickListener = new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// mIsPullToRefresh = false;
// showLoad();
// onLoadData();
// }
// };
private static final String TAG = "BaseLoadActivity";
protected void onRefreshCallback(boolean succeed) {
if (succeed && mEmptyView != null) mEmptyView.setVisibility(View.GONE);
LogUtils.LOGE(TAG + " onRefreshCallback succeed = " + succeed + " mEmptyView = " + (mEmptyView == null ? " mEmptyView is null " : mEmptyView.getVisibility()));
if (mIsPullToRefresh && mPullToRefreshLayout != null) {
if (succeed) {
mPullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
} else {
mPullToRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
}
}
}
protected void onLoadMoreCallback(boolean succeed) {
if (mPullToRefreshLayout != null) {
if (succeed) {
mPullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
} else {
mPullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.FAIL);
}
}
}
public void setRefreshState(boolean enable) {
if (mPullToRefreshLayout != null) mPullToRefreshLayout.setPullDownEnable(enable);
}
public void setEnableLoadMore(boolean flag) {
enableLoadMore = flag;
}
}