Skip to content

Commit b54b7d1

Browse files
committed
Client:优化评论请求和显示
1 parent 8c110bd commit b54b7d1

9 files changed

Lines changed: 104 additions & 65 deletions

File tree

APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/res/layout/comment_main_item.xml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,21 @@
33
style="@style/ll_vertical_match_wrap"
44
android:background="@color/white" >
55

6-
<RelativeLayout style="@style/match_wrap" >
6+
<RelativeLayout
7+
style="@style/match_wrap"
8+
android:paddingRight="10dp" >
79

810
<ImageView
911
android:id="@+id/ivCommentHead"
1012
android:layout_width="30dp"
1113
android:layout_height="30dp"
12-
android:layout_marginLeft="25dp"
13-
android:layout_marginRight="10dp"
14-
android:layout_marginTop="10dp" />
14+
android:layout_margin="10dp" />
1515

1616
<LinearLayout
1717
android:id="@+id/layout_comment_head"
1818
style="@style/ll_horizontal_match_wrap"
1919
android:layout_alignBottom="@id/ivCommentHead"
2020
android:layout_alignTop="@id/ivCommentHead"
21-
android:layout_marginRight="10dp"
2221
android:layout_toRightOf="@+id/ivCommentHead" >
2322

2423
<TextView
@@ -65,18 +64,19 @@
6564

6665
<LinearLayout
6766
android:id="@+id/llCommentContainer"
68-
style="@style/ll_vertical_match_wrap"
69-
android:paddingRight="10dp" >
67+
style="@style/ll_vertical_match_wrap" >
7068
</LinearLayout>
7169

7270
<TextView
7371
android:id="@+id/tvCommentMore"
7472
style="@style/text_middle"
7573
android:layout_width="match_parent"
7674
android:layout_gravity="right|center_vertical"
77-
android:gravity="right"
78-
android:padding="12dp"
79-
android:text="查看更多"
75+
android:background="@drawable/bg_item_to_alpha"
76+
android:gravity="left|center_vertical"
77+
android:paddingBottom="4dp"
78+
android:paddingTop="4dp"
79+
android:text="查看所有"
8080
android:textColor="@color/green_deep"
8181
android:textSize="13dp"
8282
android:visibility="gone" />

APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/res/layout/moment_view.xml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@
8787
<LinearLayout
8888
android:id="@+id/llMomentViewPraise"
8989
style="@style/ll_horizontal_wrap_wrap"
90+
android:gravity="center"
9091
android:background="@drawable/bg_item_to_alpha"
91-
android:padding="5dp" >
92+
android:paddingLeft="5dp"
93+
android:paddingRight="5dp" >
9294

9395
<ImageView
9496
android:id="@+id/ivMomentViewPraise"
@@ -105,15 +107,16 @@
105107

106108
<View
107109
style="@style/divider_vertical_1px"
108-
android:layout_marginBottom="10dp"
109-
android:layout_marginTop="10dp" />
110+
android:layout_marginBottom="5dp"
111+
android:layout_marginTop="5dp" />
110112

111113
<LinearLayout
114+
android:gravity="center"
112115
android:id="@+id/llMomentViewComment"
113116
style="@style/ll_horizontal_wrap_wrap"
114-
android:layout_marginLeft="6dp"
115117
android:background="@drawable/bg_item_to_alpha"
116-
android:padding="5dp" >
118+
android:paddingLeft="5dp"
119+
android:paddingRight="5dp" >
117120

118121
<ImageView
119122
android:id="@+id/ivMomentViewComment"
@@ -132,7 +135,8 @@
132135
android:id="@+id/llMomentViewCommentContainer"
133136
style="@style/ll_vertical_match_wrap"
134137
android:layout_marginTop="5dp"
135-
android:background="@color/alpha_3" >
138+
android:background="@color/alpha_3"
139+
android:visibility="gone" >
136140
</LinearLayout>
137141
</LinearLayout>
138142
</LinearLayout>

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

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package apijson.demo.client.activity_fragment;
1616

1717
import java.util.ArrayList;
18-
import java.util.HashMap;
18+
import java.util.LinkedHashMap;
1919
import java.util.List;
2020
import java.util.Map;
2121

@@ -49,6 +49,7 @@
4949
import apijson.demo.client.adapter.CommentAdapter;
5050
import apijson.demo.client.adapter.CommentAdapter.ItemView.OnCommentClickListener;
5151
import apijson.demo.client.application.APIJSONApplication;
52+
import apijson.demo.client.manager.HttpManager;
5253
import apijson.demo.client.model.Comment;
5354
import apijson.demo.client.model.CommentItem;
5455
import apijson.demo.client.model.MomentItem;
@@ -353,21 +354,30 @@ private void sendMomentBroadcast(int actionType) {
353354
private boolean loadHead = true;
354355
@Override
355356
public void getListAsync(final int page) {
356-
if (loadHead && page <= 0) {
357+
if (loadHead && page <= HttpManager.PAGE_NUM_0) {
357358
HttpRequest.getMoment(momentId, HTTP_GET_MOMENT, MomentActivity.this);
358359
}
359-
HttpRequest.getCommentList(momentId, 4, page, -page, this);
360+
HttpRequest.getCommentList(momentId, 0, page, -page, this);
360361
}
361362

362-
//TODO 还是直接在Request中获取childList?
363+
// private int page;
363364
@Override
364365
public List<CommentItem> parseArray(String json) {
366+
365367
List<CommentItem> list = JSON.parseArray(new JSONResponse(json).getArray(
366368
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+
367377
if (list != null && list.isEmpty() == false) {
368378
//parent和child分类
369-
Map<Long, CommentItem> parentMap = new HashMap<>();//added
370-
Map<Long, CommentItem> allChildMap = new HashMap<>();
379+
Map<Long, CommentItem> parentMap = new LinkedHashMap<>();//added
380+
Map<Long, CommentItem> allChildMap = new LinkedHashMap<>();
371381
long id;
372382
long toId;
373383
for (CommentItem item : list) {
@@ -376,7 +386,7 @@ public List<CommentItem> parseArray(String json) {
376386
continue;
377387
}
378388

379-
toId = item.getComment().getToId();
389+
toId = item.getToId();
380390
if (toId <= 0) {//parent
381391
parentMap.put(id, item);
382392
} else {//child
@@ -385,28 +395,37 @@ public List<CommentItem> parseArray(String json) {
385395
}
386396

387397
//child放到parent的childList中
398+
boolean isFirst;
388399
CommentItem parent;
389400
List<CommentItem> childList;
390401
for (final CommentItem child : allChildMap.values()) {
391-
parent = child;
392-
do {//根据父评论一步步找到一级父评论
393-
toId = parent.getComment().getToId();//父评论的父评论的id
402+
toId = child.getToId();
403+
isFirst = true;
404+
while (parentMap.containsKey(toId) == false) {//根据父评论一步步找到一级父评论
394405
parent = toId <= 0 ? null : allChildMap.get(toId);
395406
if (parent == null) {
396407
break;
397408
}
398-
} while (parentMap.containsKey(toId) == false);//toId不是一级父评论id
399-
409+
if (isFirst) {
410+
isFirst = false;
411+
child.setToUser(parent.getUser());
412+
}
413+
414+
toId = parent.getToId();//父评论的父评论的id
415+
}
416+
400417
parent = parentMap.get(toId);
401418
if (parent == null) {
402419
continue;
403420
}
421+
if (toId == child.getToId()) {
422+
child.setToUser(parent.getUser());
423+
}
404424

405425
childList = parent.getChildList();
406426
if (childList == null) {
407427
childList = new ArrayList<>();
408428
}
409-
child.setToUser(parent.getUser());
410429
childList.add(child);
411430

412431
parent.setChildList(childList);
@@ -554,6 +573,7 @@ public void onHttpResponse(int requestCode, String resultJson, Exception e) {
554573
return;
555574
}
556575
if (requestCode <= 0) {
576+
// this.page = -requestCode;
557577
super.onHttpResponse(requestCode, resultJson, e);
558578
return;
559579
}

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,14 @@ public long getItemId(int position) {
5555
return getItem(position).getId();
5656
}
5757

58+
private boolean showAll = false;
59+
public void setShowAll(boolean showAll) {
60+
this.showAll = showAll;
61+
}
62+
5863
@Override
5964
public ItemView createView(int position, ViewGroup parent) {
60-
return new ItemView(context, resources).setOnCommentClickListener(onCommentClickListener);
65+
return new ItemView(context, resources, showAll).setOnCommentClickListener(onCommentClickListener);
6166
}
6267

6368

@@ -70,13 +75,17 @@ public interface OnCommentClickListener {
7075
}
7176

7277
private OnCommentClickListener onCommentClickListener;
78+
7379
public ItemView setOnCommentClickListener(OnCommentClickListener onCommentClickListener) {
7480
this.onCommentClickListener = onCommentClickListener;
7581
return this;
7682
}
7783

78-
public ItemView(Activity context, Resources resources) {
84+
85+
private boolean showAll;
86+
public ItemView(Activity context, Resources resources, boolean showAll) {
7987
super(context, resources);
88+
this.showAll = showAll;
8089
}
8190

8291
private LayoutInflater inflater;
@@ -117,7 +126,7 @@ public void bindView(CommentItem data){
117126
tvCommentContent.setText("" + content);
118127
tvCommentTime.setText("" + TimeUtil.getSmartDate(data.getDate()));
119128
ImageLoaderUtil.loadImage(ivCommentHead, data.getUser().getHead(), ImageLoaderUtil.TYPE_OVAL);
120-
setCommentDownComment(data, false);
129+
setChildComment(data, showAll);
121130
}
122131

123132
@Override
@@ -141,7 +150,7 @@ public void onClick(View v) {
141150
* @param data
142151
*/
143152
@SuppressLint("InflateParams")
144-
public void setCommentDownComment(final CommentItem parentItem, boolean showAll) {
153+
public void setChildComment(final CommentItem parentItem, boolean showAll) {
145154

146155
List<CommentItem> downList = parentItem.getChildList();
147156
if (downList == null || downList.isEmpty()) {
@@ -151,18 +160,17 @@ public void setCommentDownComment(final CommentItem parentItem, boolean showAll)
151160
findViewById(R.id.vCommentItemDivider).setVisibility(View.VISIBLE);
152161

153162
tvCommentMore.setVisibility(View.GONE);
154-
if (showAll == false && downList.size() > 4) {
155-
163+
if (showAll == false && downList.size() > 3) {
156164
tvCommentMore.setText("查看更多");
157165
tvCommentMore.setVisibility(View.VISIBLE);
158166
tvCommentMore.setOnClickListener(new View.OnClickListener() {
159167
@Override
160168
public void onClick(View v) {
161-
setCommentDownComment(parentItem, true);
169+
setChildComment(parentItem, true);
162170
}
163171
});
164172

165-
downList = downList.subList(0, 4);
173+
downList = downList.subList(0, 3);
166174
}
167175

168176
llCommentContainer.removeAllViews();

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
import java.util.List;
1818

19-
import zuo.biao.apijson.JSONObject;
20-
import zuo.biao.apijson.JSONResponse;
2119
import android.support.annotation.NonNull;
2220
import apijson.demo.client.server.model.BaseModel;
2321

@@ -27,7 +25,7 @@ public class CommentItem extends BaseModel {
2725
private Comment comment;
2826
private User user;
2927
private User toUser;
30-
private JSONObject toUserObject;
28+
// private JSONObject toUserObject;
3129
private List<CommentItem> childList;
3230

3331
public CommentItem() {
@@ -64,21 +62,21 @@ public CommentItem setUser(User user) {
6462
@NonNull
6563
public User getToUser() {
6664
if (toUser == null) {
67-
toUser = JSONResponse.toObject(new JSONResponse(toUserObject), User.class);
65+
toUser = new User();//JSONResponse.toObject(new JSONResponse(toUserObject), User.class);
6866
}
6967
return toUser;
7068
}
7169
public CommentItem setToUser(User toUser) {
7270
this.toUser = toUser;
73-
setToUserObject(new JSONObject(toUser));
71+
// setToUserObject(new JSONObject(toUser));
7472
return this;
7573
}
76-
public JSONObject getToUserObject() {
77-
return toUserObject;
78-
}
79-
public void setToUserObject(JSONObject toUserObject) {
80-
this.toUserObject = toUserObject;
81-
}
74+
// public JSONObject getToUserObject() {
75+
// return toUserObject;
76+
// }
77+
// public void setToUserObject(JSONObject toUserObject) {
78+
// this.toUserObject = toUserObject;
79+
// }
8280

8381
public List<CommentItem> getChildList() {
8482
return childList;
@@ -93,6 +91,9 @@ public CommentItem setChildList(List<CommentItem> childList) {
9391
public Long getId() {
9492
return getComment().getId();
9593
}
94+
public Long getToId() {
95+
return getComment().getToId();
96+
}
9697
@Override
9798
public Long getDate() {
9899
return getComment().getDate();

APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/util/HttpRequest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,10 @@ public static void getMomentList(int range, long id, com.alibaba.fastjson.JSONOb
414414
commentItem.put(Comment.class.getSimpleName(), new JSONRequest(MOMENT_ID_AT, "[]/Moment/id"));
415415
commentItem.put(User.class.getSimpleName(), new JSONRequest(ID_AT, "/Comment/userId").setColumns(COLUMNS_USER));
416416

417-
JSONObject toUserObject = new JSONObject();
418-
toUserObject.put(User.class.getSimpleName()
419-
, new JSONRequest(ID_AT, "[]/CommentItem[]/Comment/toUserId").setColumns(COLUMNS_USER_SIMPLE));
420-
commentItem.put("toUserObject", toUserObject);
417+
// JSONObject toUserObject = new JSONObject();
418+
// toUserObject.put(User.class.getSimpleName()
419+
// , new JSONRequest(ID_AT, "[]/CommentItem[]/Comment/toUserId").setColumns(COLUMNS_USER_SIMPLE));
420+
// commentItem.put("toUserObject", toUserObject);
421421

422422
// commentItem.put("release", "total:[]/Moment/commentCount");
423423
request.add(commentItem.toArray(3, 0, CommentItem.class.getSimpleName()));

APIJSON(Android)/APIJSON(ADT)/APIJSONApp/ZBLibrary/src/zuo/biao/library/base/BaseAdapter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public BaseAdapter(Activity context) {
6363
* 传进来的数据列表
6464
*/
6565
public List<T> list;
66+
public List<T> getList() {
67+
return list;
68+
}
6669
/**刷新列表
6770
*/
6871
public synchronized void refresh(List<T> list) {

APIJSON(Android)/APIJSON(ADT)/APIJSONApp/ZBLibrary/src/zuo/biao/library/util/SettingUtil.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,20 @@ private SettingUtil() {/*不能实例化**/}
6767
public static boolean isOnTestMode = false;//测试模式
6868
public static boolean isFirstStart = true;//第一次打开应用
6969

70-
public static final boolean[] defaultValues = new boolean[]{
71-
cache,//开启缓存
72-
preload,//开启预加载
73-
74-
voice,//开启通知声
75-
vibrate,//开启震动
76-
noDisturb,//夜间防打扰
77-
78-
isOnTestMode,//测试模式
79-
isFirstStart,//第一次打开应用
80-
};
70+
public static final boolean[] defaultValues;
71+
static {
72+
defaultValues = new boolean[]{
73+
cache,//开启缓存
74+
preload,//开启预加载
75+
76+
voice,//开启通知声
77+
vibrate,//开启震动
78+
noDisturb,//夜间防打扰
79+
80+
isOnTestMode,//测试模式
81+
isFirstStart,//第一次打开应用
82+
};
83+
}
8184

8285
private static Context context;
8386
/**初始化

0 commit comments

Comments
 (0)