Skip to content

Commit 39224d4

Browse files
committed
Client:新增点击评论内名字跳转到用户详情界面
1 parent 10555b4 commit 39224d4

3 files changed

Lines changed: 59 additions & 77 deletions

File tree

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public class CommentItem extends BaseModel {
2525
private Comment comment;
2626
private User user;
2727
private User toUser;
28-
// private JSONObject toUserObject;
2928
private List<CommentItem> childList;
3029

3130
public CommentItem() {
@@ -68,15 +67,8 @@ public User getToUser() {
6867
}
6968
public CommentItem setToUser(User toUser) {
7069
this.toUser = toUser;
71-
// setToUserObject(new JSONObject(toUser));
7270
return this;
7371
}
74-
// public JSONObject getToUserObject() {
75-
// return toUserObject;
76-
// }
77-
// public void setToUserObject(JSONObject toUserObject) {
78-
// this.toUserObject = toUserObject;
79-
// }
8072

8173
public List<CommentItem> getChildList() {
8274
return childList;
@@ -102,6 +94,9 @@ public Long getDate() {
10294
public Long getUserId() {
10395
return getUser().getId();
10496
}
97+
public Long getToUserId() {
98+
return getToUser().getId();
99+
}
105100

106101
}
107102

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ public void bindView(List<CommentItem> list){
9292
setComment(list);
9393
}
9494

95-
95+
9696
private int maxShowCount = 3;
9797
public void setMaxShowCount(int maxShowCount) {
9898
this.maxShowCount = maxShowCount;
9999
}
100-
101-
100+
101+
102102
/**设置评论
103103
* @param joined
104104
* @param commentCount
@@ -107,7 +107,7 @@ public void setMaxShowCount(int maxShowCount) {
107107
public void setComment(List<CommentItem> list) {
108108
int count = list == null ? 0 : list.size();
109109
boolean showMore = maxShowCount > 0 && count > maxShowCount;
110-
110+
111111
tvCommentContainerViewMore.setVisibility(showMore ? View.VISIBLE : View.GONE);
112112

113113
llCommentContainerViewContainer.removeAllViews();
@@ -142,8 +142,8 @@ private void addCommentView(final int index, final CommentItem comment) {
142142
}
143143

144144
CommentTextView commentView = (CommentTextView) inflater.inflate(R.layout.moment_view_comment_item, null);
145-
commentView.setView(comment, true);
146-
145+
commentView.setView(comment);
146+
147147
if (onCommentClickListener != null) {
148148
commentView.setOnClickListener(new OnClickListener() {
149149

@@ -192,14 +192,6 @@ public boolean onLongClick(View v) {
192192

193193
//Event事件监听区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
194194

195-
//
196-
// @Override
197-
// public void onClick(View v) {
198-
// switch (v.getId()) {
199-
// case R.id.tvCommentContainerViewMore:
200-
// break;
201-
// }
202-
// }
203195

204196
//Event事件监听区(只要存在事件监听代码就是)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
205197

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

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@
1414

1515
package apijson.demo.client.view;
1616

17+
import zuo.biao.library.util.CommonUtil;
1718
import zuo.biao.library.util.StringUtil;
19+
import android.app.Activity;
1820
import android.content.Context;
19-
import android.graphics.Color;
2021
import android.text.SpannableString;
2122
import android.text.Spanned;
2223
import android.text.TextPaint;
23-
import android.text.style.ClickableSpan;
24+
import android.text.method.LinkMovementMethod;
2425
import android.util.AttributeSet;
2526
import android.view.View;
2627
import android.widget.TextView;
28+
import apijson.demo.client.R;
29+
import apijson.demo.client.activity_fragment.UserActivity;
2730
import apijson.demo.client.model.Comment;
2831
import apijson.demo.client.model.CommentItem;
2932
import apijson.demo.client.model.User;
@@ -32,6 +35,14 @@
3235
*/
3336
public class CommentTextView extends TextView {
3437

38+
public interface OnNameClickListener {
39+
void onNameClick(int index, View widget);
40+
}
41+
private OnNameClickListener listener;
42+
public void setOnNameClickListener(OnNameClickListener l) {
43+
this.listener = l;
44+
}
45+
3546

3647
public CommentTextView(Context context) {
3748
super(context);
@@ -45,18 +56,15 @@ public CommentTextView(Context context, AttributeSet attrs, int defStyle) {
4556
super(context, attrs, defStyle);
4657
}
4758

48-
private OnClickListener listener;
49-
public void setOnNameClickListener(OnClickListener l) {
50-
this.listener = l;
51-
}
59+
5260

5361
private Comment comment;
5462
private User user;
5563
private User toUser;
5664
/**设置View
5765
* @param comment
5866
*/
59-
public void setView(CommentItem item, boolean showCommenter) {
67+
public void setView(CommentItem item) {
6068
if (item == null) {
6169
item = new CommentItem();
6270
}
@@ -66,62 +74,49 @@ public void setView(CommentItem item, boolean showCommenter) {
6674
String content = StringUtil.getTrimedString(comment.getContent());
6775

6876
String userName = StringUtil.getTrimedString(user.getName());
69-
int userNameLength = showCommenter ? userName.length() : 0;
77+
int userNameLength = userName.length();
7078

7179
String toUserName = StringUtil.getTrimedString(toUser.getName());
7280

81+
7382
SpannableString msp = null;
7483
if (toUser.getId() <= 0) {
75-
msp = new SpannableString((showCommenter ? userName + ": " : "" ) + content);
76-
msp.setSpan(new ClickableSpan() {
77-
@Override
78-
public void updateDrawState(TextPaint ds) {
79-
ds.setColor(Color.parseColor("#009ed3")); // 设置超链接颜色
80-
ds.setUnderlineText(false); // 超链接去掉下划线
81-
}
82-
@Override
83-
public void onClick(View widget) {
84-
if (listener != null) {
85-
listener.onClick(widget);
86-
}
87-
}
88-
}, 0, userNameLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
84+
msp = new SpannableString(userName + " : " + content);
8985
} else {
90-
msp = new SpannableString((showCommenter ? userName : "") + "回复 " + toUserName
91-
+ ": " + content);
92-
msp.setSpan(new ClickableSpan() {
93-
@Override
94-
public void updateDrawState(TextPaint ds) {
95-
ds.setColor(Color.parseColor("#009ed3")); // 设置超链接颜色
96-
ds.setUnderlineText(false); // 超链接去掉下划线
97-
}
98-
99-
@Override
100-
public void onClick(View widget) {
101-
if (listener != null) {
102-
listener.onClick(widget);
103-
}
104-
}
105-
}, 0, userNameLength, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
106-
107-
msp.setSpan(new ClickableSpan() {
108-
@Override
109-
public void updateDrawState(TextPaint ds) {
110-
ds.setColor(Color.parseColor("#009ed3")); // 设置超链接颜色
111-
ds.setUnderlineText(false); // 超链接去掉下划线
112-
}
113-
114-
@Override
115-
public void onClick(View widget) {
116-
if (listener != null) {
117-
listener.onClick(widget);
118-
}
119-
}
120-
}, userNameLength + 3, userNameLength + 3 + toUserName.length(),
121-
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
86+
msp = new SpannableString(userName + " 回复 " + toUserName + " : " + content);
87+
setSpan(msp, 1, userNameLength + 4, userNameLength + 4 + toUserName.length());
12288
}
89+
setSpan(msp, 0, 0, userNameLength);
12390

12491
setText(msp);
125-
// setMovementMethod(LinkMovementMethod.getInstance());
92+
setMovementMethod(LinkMovementMethod.getInstance());
93+
}
94+
95+
private void setSpan(SpannableString msp, final int index, int start, int end) {
96+
msp.setSpan(new ClickableSpan() {
97+
98+
@Override
99+
public void onClick(View widget) {
100+
onNameClick(index, widget);
101+
}
102+
}, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
103+
}
104+
105+
106+
public void onNameClick(int index, View widget) {
107+
if (listener != null) {
108+
listener.onNameClick(index, widget);
109+
} else {
110+
CommonUtil.toActivity((Activity) getContext(), UserActivity.createIntent(getContext()
111+
, index == 0 ? user.getId() : toUser.getId()));
112+
}
113+
}
114+
115+
public abstract class ClickableSpan extends android.text.style.ClickableSpan {
116+
@Override
117+
public void updateDrawState(TextPaint ds) {
118+
ds.setColor(getResources().getColor(R.color.blue)); // 设置超链接颜色
119+
ds.setUnderlineText(false); // 超链接下划线
120+
}
126121
}
127122
}

0 commit comments

Comments
 (0)