Skip to content

Commit 69cf251

Browse files
committed
Client:优化评论显示
1 parent b54b7d1 commit 69cf251

3 files changed

Lines changed: 231 additions & 153 deletions

File tree

‎APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/activity_fragment/MomentActivity.java‎

Lines changed: 25 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import apijson.demo.client.model.CommentItem;
5555
import apijson.demo.client.model.MomentItem;
5656
import apijson.demo.client.model.User;
57+
import apijson.demo.client.util.CommentUtil;
5758
import apijson.demo.client.util.HttpRequest;
5859
import apijson.demo.client.view.MomentView;
5960

@@ -198,16 +199,31 @@ public void run() {
198199

199200
@Override
200201
public void setList(final List<CommentItem> list) {
201-
setList(new AdapterCallBack<CommentAdapter>() {
202+
runThread(TAG + "setList", new Runnable() {
202203

203204
@Override
204-
public CommentAdapter createAdapter() {
205-
return new CommentAdapter(context, MomentActivity.this);
206-
}
205+
public void run() {
206+
final List<CommentItem> list_ = CommentUtil.toDoubleLevelList(list);
207+
208+
runUiThread(new Runnable() {
209+
210+
@Override
211+
public void run() {
212+
setList(new AdapterCallBack<CommentAdapter>() {
207213

208-
@Override
209-
public void refreshAdapter() {
210-
adapter.refresh(list);
214+
@Override
215+
public CommentAdapter createAdapter() {
216+
return new CommentAdapter(context, MomentActivity.this);
217+
}
218+
219+
@Override
220+
public void refreshAdapter() {
221+
// adapter.setShowAll(true);
222+
adapter.refresh(list_);
223+
}
224+
});
225+
}
226+
});
211227
}
212228
});
213229
}
@@ -357,86 +373,12 @@ public void getListAsync(final int page) {
357373
if (loadHead && page <= HttpManager.PAGE_NUM_0) {
358374
HttpRequest.getMoment(momentId, HTTP_GET_MOMENT, MomentActivity.this);
359375
}
360-
HttpRequest.getCommentList(momentId, 0, page, -page, this);
376+
HttpRequest.getCommentList(momentId, getCacheCount(), page, -page, this);
361377
}
362378

363-
// private int page;
364379
@Override
365380
public List<CommentItem> parseArray(String json) {
366-
367-
List<CommentItem> list = JSON.parseArray(new JSONResponse(json).getArray(
368-
CommentItem.class.getSimpleName()), CommentItem.class);
369-
370-
// if (page <= HttpManager.PAGE_NUM_0) {
371-
// List<CommentItem> oringinList = adapter == null ? null : adapter.getList();
372-
// if (oringinList != null) {
373-
// list.addAll(oringinList);
374-
// }
375-
// }
376-
377-
if (list != null && list.isEmpty() == false) {
378-
//parent和child分类
379-
Map<Long, CommentItem> parentMap = new LinkedHashMap<>();//added
380-
Map<Long, CommentItem> allChildMap = new LinkedHashMap<>();
381-
long id;
382-
long toId;
383-
for (CommentItem item : list) {
384-
id = item == null ? 0 : item.getId();
385-
if (id <= 0) {
386-
continue;
387-
}
388-
389-
toId = item.getToId();
390-
if (toId <= 0) {//parent
391-
parentMap.put(id, item);
392-
} else {//child
393-
allChildMap.put(id, item);
394-
}
395-
}
396-
397-
//child放到parent的childList中
398-
boolean isFirst;
399-
CommentItem parent;
400-
List<CommentItem> childList;
401-
for (final CommentItem child : allChildMap.values()) {
402-
toId = child.getToId();
403-
isFirst = true;
404-
while (parentMap.containsKey(toId) == false) {//根据父评论一步步找到一级父评论
405-
parent = toId <= 0 ? null : allChildMap.get(toId);
406-
if (parent == null) {
407-
break;
408-
}
409-
if (isFirst) {
410-
isFirst = false;
411-
child.setToUser(parent.getUser());
412-
}
413-
414-
toId = parent.getToId();//父评论的父评论的id
415-
}
416-
417-
parent = parentMap.get(toId);
418-
if (parent == null) {
419-
continue;
420-
}
421-
if (toId == child.getToId()) {
422-
child.setToUser(parent.getUser());
423-
}
424-
425-
childList = parent.getChildList();
426-
if (childList == null) {
427-
childList = new ArrayList<>();
428-
}
429-
childList.add(child);
430-
431-
parent.setChildList(childList);
432-
parentMap.put(toId, parent);
433-
}
434-
435-
//转为list
436-
list = new ArrayList<>(parentMap.values());
437-
}
438-
439-
return list;
381+
return JSON.parseArray(new JSONResponse(json).getArray(CommentItem.class.getSimpleName()), CommentItem.class);
440382
}
441383

442384
@Override

0 commit comments

Comments
 (0)