/
UserFavorite.java
366 lines (330 loc) · 13.2 KB
/
UserFavorite.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
364
365
366
package net.oschina.app.ui;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.oschina.app.AppContext;
import net.oschina.app.AppException;
import net.oschina.app.R;
import net.oschina.app.adapter.ListViewFavoriteAdapter;
import net.oschina.app.bean.FavoriteList;
import net.oschina.app.bean.Notice;
import net.oschina.app.bean.Result;
import net.oschina.app.bean.FavoriteList.Favorite;
import net.oschina.app.common.UIHelper;
import net.oschina.app.widget.PullToRefreshListView;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
/**
* 用户收藏
* @author liux (http://my.oschina.net/liux)
* @version 1.0
* @created 2012-3-21
*/
public class UserFavorite extends BaseActivity {
private ImageView mBack;
private ProgressBar mProgressbar;
private Button favorite_catalog_software;
private Button favorite_catalog_post;
private Button favorite_catalog_code;
private Button favorite_catalog_blog;
private Button favorite_catalog_news;
private PullToRefreshListView mlvFavorite;
private ListViewFavoriteAdapter lvFavoriteAdapter;
private List<Favorite> lvFavoriteData = new ArrayList<Favorite>();
private View lvFavorite_footer;
private TextView lvFavorite_foot_more;
private ProgressBar lvFavorite_foot_progress;
private Handler mFavoriteHandler;
private int lvSumData;
private int curFavoriteCatalog;
private int curLvDataState;
private final static int DATA_LOAD_ING = 0x001;
private final static int DATA_LOAD_COMPLETE = 0x002;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_favorite);
this.initView();
this.initData();
}
/**
* 头部按钮展示
* @param type
*/
private void headButtonSwitch(int type) {
switch (type) {
case DATA_LOAD_ING:
mProgressbar.setVisibility(View.VISIBLE);
break;
case DATA_LOAD_COMPLETE:
mProgressbar.setVisibility(View.GONE);
break;
}
}
//初始化视图控件
private void initView()
{
mBack = (ImageView)findViewById(R.id.favorite_head_back);
mBack.setOnClickListener(UIHelper.finish(this));
mProgressbar = (ProgressBar)findViewById(R.id.favorite_head_progress);
favorite_catalog_software = (Button)findViewById(R.id.favorite_catalog_software);
favorite_catalog_post = (Button)findViewById(R.id.favorite_catalog_post);
favorite_catalog_code = (Button)findViewById(R.id.favorite_catalog_code);
favorite_catalog_blog = (Button)findViewById(R.id.favorite_catalog_blog);
favorite_catalog_news = (Button)findViewById(R.id.favorite_catalog_news);
favorite_catalog_software.setOnClickListener(this.favoriteBtnClick(favorite_catalog_software,FavoriteList.TYPE_SOFTWARE));
favorite_catalog_post.setOnClickListener(this.favoriteBtnClick(favorite_catalog_post,FavoriteList.TYPE_POST));
favorite_catalog_code.setOnClickListener(this.favoriteBtnClick(favorite_catalog_code,FavoriteList.TYPE_CODE));
favorite_catalog_blog.setOnClickListener(this.favoriteBtnClick(favorite_catalog_blog,FavoriteList.TYPE_BLOG));
favorite_catalog_news.setOnClickListener(this.favoriteBtnClick(favorite_catalog_news,FavoriteList.TYPE_NEWS));
favorite_catalog_software.setEnabled(false);
curFavoriteCatalog = FavoriteList.TYPE_SOFTWARE;
lvFavorite_footer = getLayoutInflater().inflate(R.layout.listview_footer, null);
lvFavorite_foot_more = (TextView)lvFavorite_footer.findViewById(R.id.listview_foot_more);
lvFavorite_foot_progress = (ProgressBar)lvFavorite_footer.findViewById(R.id.listview_foot_progress);
lvFavoriteAdapter = new ListViewFavoriteAdapter(this, lvFavoriteData, R.layout.favorite_listitem);
mlvFavorite = (PullToRefreshListView)findViewById(R.id.favorite_listview);
mlvFavorite.addFooterView(lvFavorite_footer);//添加底部视图 必须在setAdapter前
mlvFavorite.setAdapter(lvFavoriteAdapter);
mlvFavorite.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//点击头部、底部栏无效
if(position == 0 || view == lvFavorite_footer) return;
Favorite fav = null;
//判断是否是TextView
if(view instanceof TextView){
fav = (Favorite)view.getTag();
}else{
TextView title = (TextView)view.findViewById(R.id.favorite_listitem_title);
fav = (Favorite)title.getTag();
}
if(fav == null) return;
//跳转
UIHelper.showUrlRedirect(view.getContext(), fav.url);
}
});
mlvFavorite.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
mlvFavorite.onScrollStateChanged(view, scrollState);
//数据为空--不用继续下面代码了
if(lvFavoriteData.size() == 0) return;
//判断是否滚动到底部
boolean scrollEnd = false;
try {
if(view.getPositionForView(lvFavorite_footer) == view.getLastVisiblePosition())
scrollEnd = true;
} catch (Exception e) {
scrollEnd = false;
}
if(scrollEnd && curLvDataState==UIHelper.LISTVIEW_DATA_MORE)
{
mlvFavorite.setTag(UIHelper.LISTVIEW_DATA_LOADING);
lvFavorite_foot_more.setText(R.string.load_ing);
lvFavorite_foot_progress.setVisibility(View.VISIBLE);
//当前pageIndex
int pageIndex = lvSumData/20;
loadLvFavoriteData(curFavoriteCatalog, pageIndex, mFavoriteHandler, UIHelper.LISTVIEW_ACTION_SCROLL);
}
}
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
mlvFavorite.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
});
mlvFavorite.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//点击头部、底部栏无效
if(position == 0 || view == lvFavorite_footer) return false;
Favorite _fav = null;
//判断是否是TextView
if(view instanceof TextView){
_fav = (Favorite)view.getTag();
}else{
TextView title = (TextView)view.findViewById(R.id.favorite_listitem_title);
_fav = (Favorite)title.getTag();
}
if(_fav == null) return false;
final Favorite fav = _fav;
final AppContext ac = (AppContext)getApplication();
//操作--删除
final int uid = ac.getLoginUid();
final Handler handler = new Handler(){
public void handleMessage(Message msg) {
if(msg.what == 1){
Result res = (Result)msg.obj;
if(res.OK()){
lvFavoriteData.remove(fav);
lvFavoriteAdapter.notifyDataSetChanged();
}
UIHelper.ToastMessage(UserFavorite.this, res.getErrorMessage());
}else{
((AppException)msg.obj).makeToast(UserFavorite.this);
}
}
};
final Thread thread = new Thread(){
public void run() {
Message msg = new Message();
try {
Result res = ac.delFavorite(uid, fav.objid, fav.type);
msg.what = 1;
msg.obj = res;
} catch (AppException e) {
e.printStackTrace();
msg.what = -1;
msg.obj = e;
}
handler.sendMessage(msg);
}
};
UIHelper.showFavoriteOptionDialog(UserFavorite.this, thread);
return true;
}
});
mlvFavorite.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() {
public void onRefresh() {
loadLvFavoriteData(curFavoriteCatalog, 0, mFavoriteHandler, UIHelper.LISTVIEW_ACTION_REFRESH);
}
});
}
//初始化控件数据
private void initData()
{
mFavoriteHandler = new Handler()
{
public void handleMessage(Message msg) {
headButtonSwitch(DATA_LOAD_COMPLETE);
if(msg.what >= 0){
FavoriteList list = (FavoriteList)msg.obj;
Notice notice = list.getNotice();
//处理listview数据
switch (msg.arg1) {
case UIHelper.LISTVIEW_ACTION_INIT:
case UIHelper.LISTVIEW_ACTION_REFRESH:
case UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG:
lvSumData = msg.what;
lvFavoriteData.clear();//先清除原有数据
lvFavoriteData.addAll(list.getFavoritelist());
break;
case UIHelper.LISTVIEW_ACTION_SCROLL:
lvSumData += msg.what;
if(lvFavoriteData.size() > 0){
for(Favorite fav1 : list.getFavoritelist()){
boolean b = false;
for(Favorite fav2 : lvFavoriteData){
if(fav1.objid == fav2.objid){
b = true;
break;
}
}
if(!b) lvFavoriteData.add(fav1);
}
}else{
lvFavoriteData.addAll(list.getFavoritelist());
}
break;
}
if(msg.what < 20){
curLvDataState = UIHelper.LISTVIEW_DATA_FULL;
lvFavoriteAdapter.notifyDataSetChanged();
lvFavorite_foot_more.setText(R.string.load_full);
}else if(msg.what == 20){
curLvDataState = UIHelper.LISTVIEW_DATA_MORE;
lvFavoriteAdapter.notifyDataSetChanged();
lvFavorite_foot_more.setText(R.string.load_more);
}
//发送通知广播
if(notice != null){
UIHelper.sendBroadCast(UserFavorite.this, notice);
}
}
else if(msg.what == -1){
//有异常--显示加载出错 & 弹出错误消息
curLvDataState = UIHelper.LISTVIEW_DATA_MORE;
lvFavorite_foot_more.setText(R.string.load_error);
((AppException)msg.obj).makeToast(UserFavorite.this);
}
if(lvFavoriteData.size()==0){
curLvDataState = UIHelper.LISTVIEW_DATA_EMPTY;
lvFavorite_foot_more.setText(R.string.load_empty);
}
lvFavorite_foot_progress.setVisibility(View.GONE);
if(msg.arg1 == UIHelper.LISTVIEW_ACTION_REFRESH){
mlvFavorite.onRefreshComplete(getString(R.string.pull_to_refresh_update) + new Date().toLocaleString());
mlvFavorite.setSelection(0);
}else if(msg.arg1 == UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG){
mlvFavorite.onRefreshComplete();
mlvFavorite.setSelection(0);
}
}
};
this.loadLvFavoriteData(curFavoriteCatalog,0,mFavoriteHandler,UIHelper.LISTVIEW_ACTION_INIT);
}
/**
* 线程加载收藏数据
* @param type 0:全部收藏 1:软件 2:话题 3:博客 4:新闻 5:代码
* @param pageIndex 当前页数
* @param handler 处理器
* @param action 动作标识
*/
private void loadLvFavoriteData(final int type,final int pageIndex,final Handler handler,final int action){
headButtonSwitch(DATA_LOAD_ING);
new Thread(){
public void run() {
Message msg = new Message();
boolean isRefresh = false;
if(action == UIHelper.LISTVIEW_ACTION_REFRESH || action == UIHelper.LISTVIEW_ACTION_SCROLL)
isRefresh = true;
try {
FavoriteList favoriteList = ((AppContext)getApplication()).getFavoriteList(type, pageIndex, isRefresh);
msg.what = favoriteList.getPageSize();
msg.obj = favoriteList;
} catch (AppException e) {
e.printStackTrace();
msg.what = -1;
msg.obj = e;
}
msg.arg1 = action;//告知handler当前action
if(curFavoriteCatalog == type)
handler.sendMessage(msg);
}
}.start();
}
private View.OnClickListener favoriteBtnClick(final Button btn,final int catalog){
return new View.OnClickListener() {
public void onClick(View v) {
if(btn == favorite_catalog_blog)
favorite_catalog_blog.setEnabled(false);
else
favorite_catalog_blog.setEnabled(true);
if(btn == favorite_catalog_code)
favorite_catalog_code.setEnabled(false);
else
favorite_catalog_code.setEnabled(true);
if(btn == favorite_catalog_news)
favorite_catalog_news.setEnabled(false);
else
favorite_catalog_news.setEnabled(true);
if(btn == favorite_catalog_post)
favorite_catalog_post.setEnabled(false);
else
favorite_catalog_post.setEnabled(true);
if(btn == favorite_catalog_software)
favorite_catalog_software.setEnabled(false);
else
favorite_catalog_software.setEnabled(true);
lvFavorite_foot_more.setText(R.string.load_more);
lvFavorite_foot_progress.setVisibility(View.GONE);
curFavoriteCatalog = catalog;
loadLvFavoriteData(curFavoriteCatalog, 0, mFavoriteHandler, UIHelper.LISTVIEW_ACTION_CHANGE_CATALOG);
}
};
}
}