Skip to content

Commit 562d027

Browse files
committed
Client:初步完成多级评论显示和交互
1 parent 856e2f6 commit 562d027

3 files changed

Lines changed: 89 additions & 8 deletions

File tree

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

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414

1515
package apijson.demo.client.activity_fragment;
1616

17+
import java.util.ArrayList;
18+
import java.util.HashMap;
1719
import java.util.List;
20+
import java.util.Map;
1821

1922
import zuo.biao.apijson.JSON;
2023
import zuo.biao.apijson.JSONResponse;
@@ -35,6 +38,7 @@
3538
import android.content.Context;
3639
import android.content.Intent;
3740
import android.os.Bundle;
41+
import android.os.Handler;
3842
import android.view.View;
3943
import android.view.View.OnClickListener;
4044
import android.widget.AdapterView;
@@ -176,7 +180,7 @@ private void setHead(final MomentItem momentItem) {
176180
@Override
177181
public void run() {
178182
momentView.bindView(momentItem);
179-
183+
180184
if (showKeyboard) {//在etMomentInput被绑定前调用showInput崩溃 //{
181185
showKeyboard = false;
182186

@@ -210,6 +214,13 @@ public void refreshAdapter() {
210214
* @param toCommentItem
211215
*/
212216
public void showInput(CommentItem toCommentItem_) {
217+
showInput(toCommentItem_, -1);
218+
}
219+
/**显示输入评论
220+
* @param toCommentItem
221+
* @param position
222+
*/
223+
public void showInput(CommentItem toCommentItem_, final int position) {
213224
this.toCommentItem = toCommentItem_;
214225
final long toCommentId = toCommentItem == null ? 0 : toCommentItem.getComment().getId();
215226
runUiThread(new Runnable() {
@@ -222,6 +233,18 @@ public void run() {
222233
etMomentInput.setHint("回复:" + StringUtil.getTrimedString(toCommentItem.getUser().getName()));
223234
}
224235
EditTextManager.showKeyboard(context, etMomentInput);//, toGetWindowTokenView);
236+
237+
if (position >= 0) {
238+
new Handler().postDelayed(new Runnable() {
239+
240+
@Override
241+
public void run() {
242+
if (isAlive()) {
243+
lvBaseList.setSelection(position + lvBaseList.getHeaderViewsCount());
244+
}
245+
}
246+
}, 500);
247+
}
225248
}
226249
});
227250
}
@@ -335,7 +358,52 @@ public void getListAsync(final int page) {
335358

336359
@Override
337360
public List<CommentItem> parseArray(String json) {
338-
return JSON.parseArray(new JSONResponse(json).getArray(CommentItem.class.getSimpleName()), CommentItem.class);
361+
List<CommentItem> list = JSON.parseArray(new JSONResponse(json).getArray(
362+
CommentItem.class.getSimpleName()), CommentItem.class);
363+
if (list != null && list.isEmpty() == false) {
364+
//parent和child分类
365+
Map<Long, CommentItem> parentMap = new HashMap<>();//added
366+
List<CommentItem> allChildList = new ArrayList<>();
367+
long id;
368+
long toId;
369+
for (CommentItem item : list) {
370+
id = item == null ? 0 : item.getId();
371+
if (id <= 0) {
372+
continue;
373+
}
374+
375+
toId = item.getComment().getParentId();
376+
if (toId <= 0) {//parent
377+
parentMap.put(id, item);
378+
} else {//child
379+
allChildList.add(item);
380+
}
381+
}
382+
383+
//child放到parent的childList中
384+
CommentItem parent;
385+
List<CommentItem> childList;
386+
for (CommentItem child : allChildList) {
387+
toId = child == null ? 0 : child.getComment().getParentId();
388+
parent = toId <= 0 ? null : parentMap.get(toId);
389+
if (parent != null) {
390+
childList = parent.getChildList();
391+
if (childList == null) {
392+
childList = new ArrayList<>();
393+
}
394+
child.setToUser(parent.getUser());
395+
childList.add(child);
396+
397+
parent.setChildList(childList);
398+
parentMap.put(toId, parent);
399+
}
400+
}
401+
402+
//转为list
403+
list = new ArrayList<>(parentMap.values());
404+
}
405+
406+
return list;
339407
}
340408

341409
@Override
@@ -437,7 +505,7 @@ private void onComemntItemClick(int position,boolean isLong) {
437505
}
438506
new AlertDialog(context, null, "删除这条评论?", true, DIALOG_DELETE_COMMENT, MomentActivity.this).show();
439507
} else {
440-
showInput(item);
508+
showInput(item, position);
441509
}
442510
}
443511

‎APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/adapter/CommentAdapter.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package apijson.demo.client.adapter;
22

3-
import java.util.ArrayList;
43
import java.util.List;
54

65
import zuo.biao.library.base.BaseView;
@@ -130,9 +129,9 @@ public void onClick(View v) {
130129
* @param data
131130
*/
132131
@SuppressLint("InflateParams")
133-
public void setCommentDownComment(final CommentItem parentCb, boolean showAll) {
132+
public void setCommentDownComment(final CommentItem parentItem, boolean showAll) {
134133

135-
List<CommentItem> downList = new ArrayList<>();// getItem(position).getChildList();
134+
List<CommentItem> downList = parentItem.getChildList();
136135
if (downList == null || downList.isEmpty()) {
137136
llDownCommentContainer.removeAllViews();
138137
findViewById(R.id.vCommentItemDivider).setVisibility(View.GONE);
@@ -147,7 +146,7 @@ public void setCommentDownComment(final CommentItem parentCb, boolean showAll) {
147146
tvWatchMore.setOnClickListener(new View.OnClickListener() {
148147
@Override
149148
public void onClick(View v) {
150-
setCommentDownComment(parentCb, true);
149+
setCommentDownComment(parentItem, true);
151150
}
152151
});
153152

@@ -162,7 +161,9 @@ public void onClick(View v) {
162161
final CommentItem data = downList.get(i);
163162
String name = StringUtil.getTrimedString(data.getUser().getName());
164163
String content = StringUtil.getTrimedString(data.getComment().getContent());
165-
tvContent.setText(Html.fromHtml("<font color=\"#25a281\">" + name + " :</font> " + content
164+
tvContent.setText(Html.fromHtml("<font color=\"#25a281\">" + name + " :</font> "
165+
+ " 回复 "
166+
+ "<font color=\"#25a281\">" + data.getToUser().getName() + " :</font> "+ content
166167
+ " <font color=\"#b5b5b5\">" + TimeUtil.getSmartDate(data.getDate()) + "</font>"));
167168

168169
commentItem.setOnClickListener(new OnClickListener() {

‎APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/model/CommentItem.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package apijson.demo.client.model;
22

3+
import java.util.List;
4+
35
import zuo.biao.apijson.JSONObject;
46
import zuo.biao.apijson.JSONResponse;
57
import android.support.annotation.NonNull;
@@ -12,6 +14,7 @@ public class CommentItem extends BaseModel {
1214
private User user;
1315
private User toUser;
1416
private JSONObject toUserObject;
17+
private List<CommentItem> childList;
1518

1619
public CommentItem() {
1720
super();
@@ -65,6 +68,15 @@ public JSONObject getToUserObject() {
6568
public void setToUserObject(JSONObject toUserObject) {
6669
this.toUserObject = toUserObject;
6770
}
71+
72+
public List<CommentItem> getChildList() {
73+
return childList;
74+
}
75+
public CommentItem setChildList(List<CommentItem> childList) {
76+
this.childList = childList;
77+
return this;
78+
}
79+
6880

6981
@Override
7082
public Long getId() {

0 commit comments

Comments
 (0)