Repository navigation
Expand file tree
/
Copy pathCommentTextView.java
More file actions
122 lines (97 loc) · 3.5 KB
/
Copy pathCommentTextView.java
File metadata and controls
122 lines (97 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*Copyright ©2016 TommyLemon(https://github.com/TommyLemon)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
package apijson.demo.client.view;
import android.app.Activity;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import apijson.demo.client.R;
import apijson.demo.client.activity_fragment.UserActivity;
import apijson.demo.client.model.Comment;
import apijson.demo.client.model.CommentItem;
import apijson.demo.client.model.User;
import zuo.biao.library.util.CommonUtil;
import zuo.biao.library.util.StringUtil;
/**评论
*/
public class CommentTextView extends TextView {
public interface OnNameClickListener {
void onNameClick(int index, View widget, User user);
}
private OnNameClickListener listener;
public void setOnNameClickListener(OnNameClickListener l) {
this.listener = l;
}
public CommentTextView(Context context) {
super(context);
}
public CommentTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CommentTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private Comment comment;
private User user;
private User toUser;
/**设置View
* @param item
*/
public void setView(CommentItem item) {
if (item == null) {
item = new CommentItem();
}
this.comment = item.getComment();
this.user = item.getUser();
this.toUser = item.getToUser();
String content = StringUtil.getTrimedString(comment.getContent());
String userName = StringUtil.getTrimedString(user.getName());
int userNameLength = userName.length();
String toUserName = StringUtil.getTrimedString(toUser.getName());
SpannableString msp = null;
if (toUser.getId() <= 0) {
msp = new SpannableString(userName + " : " + content);
} else {
msp = new SpannableString(userName + " 回复 " + toUserName + " : " + content);
setSpan(msp, 1, userNameLength + 4, userNameLength + 4 + toUserName.length(), toUser);
}
setSpan(msp, 0, 0, userNameLength, user);
setText(msp);
setMovementMethod(LinkMovementMethod.getInstance());
}
private void setSpan(SpannableString msp, final int index, int start, int end, final User user) {
msp.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
onNameClick(index, widget, user);
}
}, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
public void onNameClick(int index, View widget, User user) {
if (listener != null) {
listener.onNameClick(index, widget, user);
} else {
CommonUtil.toActivity((Activity) getContext(), UserActivity.createIntent(getContext(), user.getId()));
}
}
public abstract class ClickableSpan extends android.text.style.ClickableSpan {
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(getResources().getColor(R.color.blue)); // 设置超链接颜色
ds.setUnderlineText(false); // 超链接下划线
}
}
}