Skip to content

Commit 80ba1af

Browse files
committed
Client:优化评论
1 parent 562d027 commit 80ba1af

4 files changed

Lines changed: 53 additions & 35 deletions

File tree

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,30 @@ public class MomentActivity extends BaseHttpListActivity<CommentItem, CommentAda
8282
* @return
8383
*/
8484
public static Intent createIntent(Context context, long momentId, boolean showKeyboard) {
85-
return createIntent(context, momentId, showKeyboard, 0, null);
85+
return createIntent(context, momentId, showKeyboard, 0, 0, null);
8686
}
8787
/**启动这个Activity的Intent
8888
* @param context
8989
* @param momentId
9090
* @param toCommentId
91+
* @param toUserId
9192
* @param toUserName
9293
* @return
9394
*/
94-
public static Intent createIntent(Context context, long momentId, long toCommentId, String toUserName) {
95-
return createIntent(context, momentId, toCommentId > 0, toCommentId, toUserName);
95+
public static Intent createIntent(Context context, long momentId, long toCommentId, long toUserId, String toUserName) {
96+
return createIntent(context, momentId, toCommentId > 0, toCommentId, toUserId, toUserName);
9697
}
9798
/**启动这个Activity的Intent
9899
* @param context
99100
* @param momentId
100101
* @param showKeyboard
101102
* @param toCommentId
103+
* @param toUserId
102104
* @param toUserName
103105
* @return
104106
*/
105107
public static Intent createIntent(Context context, long momentId, boolean showKeyboard
106-
, long toCommentId, String toUserName) {
108+
, long toCommentId, long toUserId, String toUserName) {
107109
return new Intent(context, MomentActivity.class).
108110
putExtra(INTENT_MOMENT_ID, momentId).
109111
putExtra(INTENT_SHOW_KEYBOARD, showKeyboard).
@@ -214,13 +216,13 @@ public void refreshAdapter() {
214216
* @param toCommentItem
215217
*/
216218
public void showInput(CommentItem toCommentItem_) {
217-
showInput(toCommentItem_, -1);
219+
showInput(toCommentItem_, -1, -1);
218220
}
219221
/**显示输入评论
220222
* @param toCommentItem
221223
* @param position
222224
*/
223-
public void showInput(CommentItem toCommentItem_, final int position) {
225+
public void showInput(CommentItem toCommentItem_, final int position, final int index) {
224226
this.toCommentItem = toCommentItem_;
225227
final long toCommentId = toCommentItem == null ? 0 : toCommentItem.getComment().getId();
226228
runUiThread(new Runnable() {
@@ -306,14 +308,15 @@ public void run() {
306308
/**发送评论(回复)
307309
*/
308310
public void sendComment() {
309-
long toCommentId = toCommentItem == null ? -1 : toCommentItem.getId();
310311
String content = StringUtil.getTrimedString(etMomentInput);
311312
if (StringUtil.isNotEmpty(content, true) == false) {
312313
showShortToast("请先输入评论");
313314
return;
314315
}
316+
long toCommentId = toCommentItem == null ? 0 : toCommentItem.getId();
317+
long toUserId = toCommentId <= 0 ? 0 : toCommentItem.getUserId();
315318

316-
HttpRequest.addComment(momentId, toCommentId, content, toCommentId <= 0 ? HTTP_COMMENT : HTTP_REPLY, this);
319+
HttpRequest.addComment(momentId, toCommentId, toUserId, content, toCommentId <= 0 ? HTTP_COMMENT : HTTP_REPLY, this);
317320

318321
hideKeyboard();
319322
}
@@ -356,6 +359,7 @@ public void getListAsync(final int page) {
356359
HttpRequest.getCommentList(momentId, 4, page, -page, this);
357360
}
358361

362+
//TODO 还是直接在Request中获取childList?
359363
@Override
360364
public List<CommentItem> parseArray(String json) {
361365
List<CommentItem> list = JSON.parseArray(new JSONResponse(json).getArray(
@@ -391,7 +395,7 @@ public List<CommentItem> parseArray(String json) {
391395
if (childList == null) {
392396
childList = new ArrayList<>();
393397
}
394-
child.setToUser(parent.getUser());
398+
child.setToUser(parent.getUser());//TODO 不一定是parent的User
395399
childList.add(child);
396400

397401
parent.setChildList(childList);
@@ -484,7 +488,7 @@ public boolean onItemLongClick(AdapterView<?> parent, View view, int position, l
484488
});
485489
}
486490

487-
private void onComemntItemClick(int position,boolean isLong) {
491+
private void onComemntItemClick(int position, boolean isLong) {
488492
if (adapter == null){
489493
return;
490494
}
@@ -493,7 +497,11 @@ private void onComemntItemClick(int position,boolean isLong) {
493497
return;
494498
}
495499

496-
CommentItem item = adapter.getItem(position);
500+
onCommentClick(adapter.getItem(position), position, -1, isLong);
501+
}
502+
503+
@Override
504+
public void onCommentClick(CommentItem item, int position, int index, boolean isLong) {
497505
if (isLong) {
498506
if (item == null || momentItem == null) {
499507
return;
@@ -505,13 +513,8 @@ private void onComemntItemClick(int position,boolean isLong) {
505513
}
506514
new AlertDialog(context, null, "删除这条评论?", true, DIALOG_DELETE_COMMENT, MomentActivity.this).show();
507515
} else {
508-
showInput(item, position);
509-
}
510-
}
511-
512-
@Override
513-
public void onCommentClick(CommentItem cb) {
514-
516+
showInput(item, position, index);
517+
}
515518
}
516519

517520
private static final int DIALOG_DELETE_COMMENT = 1;

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.view.LayoutInflater;
1515
import android.view.View;
1616
import android.view.View.OnClickListener;
17+
import android.view.View.OnLongClickListener;
1718
import android.view.ViewGroup;
1819
import android.widget.ImageView;
1920
import android.widget.LinearLayout;
@@ -45,13 +46,13 @@ public ItemView createView(int position, ViewGroup parent) {
4546
return new ItemView(context, resources).setOnCommentClickListener(onCommentClickListener);
4647
}
4748

48-
49+
4950
public static class ItemView extends BaseView<CommentItem> implements OnClickListener {
5051

5152
/**
5253
*/
5354
public interface OnCommentClickListener {
54-
void onCommentClick(CommentItem data);
55+
void onCommentClick(CommentItem item, int position, int index, boolean isLong);
5556
}
5657

5758
private OnCommentClickListener onCommentClickListener;
@@ -82,12 +83,12 @@ public View createView(LayoutInflater inflater) {
8283
this.inflater = inflater;
8384
convertView = inflater.inflate(R.layout.comment_main_item, null);
8485

85-
ivHead = findViewById(R.id.ivCommentHead);
86+
ivHead = findViewById(R.id.ivCommentHead, this);
8687
llDownCommentContainer = findViewById(R.id.llCommentDownCommentContainer);
8788
tvWatchMore = findViewById(R.id.tvCommentWatchMore);
8889
rlCommentMainItem = findViewById(R.id.rlCommentMainItem);
8990

90-
tvName = (TextView) findViewById(R.id.tvCommentName);
91+
tvName = (TextView) findViewById(R.id.tvCommentName, this);
9192
tvContent = (TextView) findViewById(R.id.tvCommentContent);
9293
tvTime = (TextView) findViewById(R.id.tvCommentTime);
9394

@@ -113,7 +114,7 @@ public void onClick(View v) {
113114
switch (v.getId()) {
114115
case R.id.tvCommentContent:
115116
if (onCommentClickListener != null) {
116-
onCommentClickListener.onCommentClick(data);
117+
onCommentClickListener.onCommentClick(data, position, -1, false);
117118
}
118119
break;
119120
case R.id.ivCommentHead:
@@ -141,7 +142,7 @@ public void setCommentDownComment(final CommentItem parentItem, boolean showAll)
141142
tvWatchMore.setVisibility(View.GONE);
142143
if (showAll == false && downList.size() > 4) {
143144

144-
tvWatchMore.setText("查看更多" + (downList.size() - 4) + "条回复");// String.valueOf(downList.size() - 4) + "条回复");
145+
tvWatchMore.setText("查看更多");
145146
tvWatchMore.setVisibility(View.VISIBLE);
146147
tvWatchMore.setOnClickListener(new View.OnClickListener() {
147148
@Override
@@ -155,32 +156,44 @@ public void onClick(View v) {
155156

156157
llDownCommentContainer.removeAllViews();
157158
for (int i = 0; i < downList.size(); i++) {
159+
final int index = i;
160+
158161
View commentItem = inflater.inflate(R.layout.comment_down_item, null);
159162
TextView tvContent = (TextView) commentItem.findViewById(R.id.tvCommentContent);
160163

161164
final CommentItem data = downList.get(i);
162165
String name = StringUtil.getTrimedString(data.getUser().getName());
163166
String content = StringUtil.getTrimedString(data.getComment().getContent());
164-
tvContent.setText(Html.fromHtml("<font color=\"#25a281\">" + name + " :</font> "
165-
+ " 回复 "
166-
+ "<font color=\"#25a281\">" + data.getToUser().getName() + " :</font> "+ content
167-
+ " <font color=\"#b5b5b5\">" + TimeUtil.getSmartDate(data.getDate()) + "</font>"));
167+
tvContent.setText(Html.fromHtml("<font color=\"#25a281\">" + StringUtil.getString(name) + "</font>"
168+
+ " 回复 " + "<font color=\"#25a281\">" + StringUtil.getString(data.getToUser().getName())
169+
+ "</font>" + " : " + content));
168170

169171
commentItem.setOnClickListener(new OnClickListener() {
170172
@Override
171173
public void onClick(View v) {
172-
if (onCommentClickListener != null) {
173-
onCommentClickListener.onCommentClick(data);
174-
}
174+
onCommentClick(data, position, index, false);
175+
176+
}
177+
});
178+
commentItem.setOnLongClickListener(new OnLongClickListener() {
179+
180+
@Override
181+
public boolean onLongClick(View v) {
182+
onCommentClick(data, position, index, true);
183+
return true;
175184
}
176185
});
177186

178187
llDownCommentContainer.addView(commentItem);
179188
}
180189
}
181190
}
182-
183-
}
184191

192+
protected void onCommentClick(CommentItem item, int position, int index, boolean isLong) {
193+
if (onCommentClickListener != null) {
194+
onCommentClickListener.onCommentClick(item, position, index, isLong);
195+
}
196+
}
197+
}
185198

186199
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,16 +493,18 @@ public static void getWallet(int requestCode, OnHttpResponseListener listener) {
493493
/**
494494
* @param momentId
495495
* @param toCommentId
496+
* @param toUserId 不能省,可能同一toCommentId下回复不同的人
496497
* @param content
497498
* @param requestCode
498499
* @param listener
499500
*/
500-
public static void addComment(long momentId, long toCommentId, String content
501+
public static void addComment(long momentId, long toCommentId, long toUserId, String content
501502
, int requestCode, OnHttpResponseListener listener) {
502503
Comment comment = new Comment()
503504
.setUserId(application.getCurrentUserId())
504505
.setMomentId(momentId)
505506
.setParentId(toCommentId)
507+
.setToUserId(toUserId)
506508
.setContent(content);
507509
post(new JSONRequest(comment).setTag(Comment.class.getSimpleName()), requestCode, listener);
508510
}

‎APIJSON(Android)/APIJSON(ADT)/APIJSONApp/APIJSONApp/src/apijson/demo/client/view/MomentView.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ private void toComment(CommentItem commentItem, boolean isToComment) {
325325
commentItem = new CommentItem();
326326
}
327327
toActivity(MomentActivity.createIntent(context, momentId, isToComment
328-
, commentItem.getId(), commentItem.getUser().getName()));
328+
, commentItem.getId(), commentItem.getUser().getId(), commentItem.getUser().getName()));
329329
}
330330

331331
//UI显示区(操作UI,但不存在数据获取或处理代码,也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

0 commit comments

Comments
 (0)