-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathCommentFun.java
More file actions
209 lines (187 loc) · 7.99 KB
/
CommentFun.java
File metadata and controls
209 lines (187 loc) · 7.99 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.example.QzoneComment;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.*;
import com.example.QzoneComment.model.Comment;
import com.example.QzoneComment.model.User;
import java.util.ArrayList;
/**
* 评论相关方法
*/
public class CommentFun {
public static final int KEY_COMMENT_SOURCE_COMMENT_LIST = -200162;
/**
* 在页面中显示评论列表
*
* @param context
* @param mCommentList
* @param commentList
* @param btnComment
* @param tagHandler
*/
public static void parseCommentList(Context context, ArrayList<Comment> mCommentList, LinearLayout commentList,
View btnComment, Html.TagHandler tagHandler) {
if (btnComment != null) {
btnComment.setTag(KEY_COMMENT_SOURCE_COMMENT_LIST, mCommentList);
}
TextView textView;
Comment comment;
int i;
String content;
for (i = 0; i < mCommentList.size(); i++) {
comment = mCommentList.get(i);
textView = (TextView) commentList.getChildAt(i);
if (textView == null) { // 创建评论Videw
textView = (TextView) View.inflate(context, R.layout.view_comment_list_item, null);
commentList.addView(textView);
}
textView.setVisibility(View.VISIBLE);
if (comment.mReceiver == null) { // 没有评论接受者
content = String.format("<html><%s>%s</%s>: <%s>%s</%s></html>", CustomTagHandler.TAG_COMMENTATOR,
comment.mCommentator.mName, CustomTagHandler.TAG_COMMENTATOR,
CustomTagHandler.TAG_CONTENT, comment.mContent, CustomTagHandler.TAG_CONTENT);
} else {
content = String.format("<html><%s>%s</%s> 回复 <%s>%s</%s>: <%s>%s</%s><html>",
CustomTagHandler.TAG_COMMENTATOR, comment.mCommentator.mName, CustomTagHandler.TAG_COMMENTATOR,
CustomTagHandler.TAG_RECEIVER, comment.mReceiver.mName, CustomTagHandler.TAG_RECEIVER,
CustomTagHandler.TAG_CONTENT, comment.mContent, CustomTagHandler.TAG_CONTENT);
}
textView.setText(Html.fromHtml(content, null, tagHandler)); // 解析标签
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setTag(CustomTagHandler.KEY_COMMENTATOR, comment.mCommentator);
textView.setTag(CustomTagHandler.KEY_RECEIVER, comment.mReceiver);
textView.setTag(KEY_COMMENT_SOURCE_COMMENT_LIST, mCommentList);
}
for (; i < commentList.getChildCount(); i++) {
commentList.getChildAt(i).setVisibility(View.GONE);
}
if (mCommentList.size() > 0) {
commentList.setVisibility(View.VISIBLE);
} else {
commentList.setVisibility(View.GONE);
}
}
/**
* 弹出评论对话框
*/
public static void inputComment(final Activity activity, final ListView listView,
final View btnComment, final User receiver,
final InputCommentListener listener) {
final ArrayList<Comment> commentList = (ArrayList) btnComment.getTag(KEY_COMMENT_SOURCE_COMMENT_LIST);
String hint;
if (receiver != null) {
if (receiver.mId == MainActivity.sUser.mId) {
hint = "我也说一句";
} else {
hint = "回复 " + receiver.mName;
}
} else {
hint = "我也说一句";
}
// 获取评论的位置,不要在CommentDialogListener.onShow()中获取,onShow在输入法弹出后才调用,
// 此时btnComment所属的父布局可能已经被ListView回收
final int[] coord = new int[2];
if (listView != null) {
btnComment.getLocationOnScreen(coord);
}
//弹出评论对话框
showInputComment(activity, hint, new CommentDialogListener() {
@Override
public void onClickPublish(final Dialog dialog, EditText input, final TextView btn) {
final String content = input.getText().toString();
if (content.trim().equals("")) {
Toast.makeText(activity, "评论不能为空", Toast.LENGTH_SHORT).show();
return;
}
btn.setClickable(false);
final long receiverId = receiver == null ? -1 : receiver.mId;
Comment comment = new Comment(MainActivity.sUser, content, receiver);
commentList.add(comment);
if (listener != null) {
listener.onCommitComment();
}
dialog.dismiss();
Toast.makeText(activity, "评论成功", Toast.LENGTH_SHORT).show();
}
/**
* @see CommentDialogListener
* @param inputViewCoordinatesInScreen [left,top]
*/
@Override
public void onShow(int[] inputViewCoordinatesInScreen) {
if (listView != null) {
// 点击某条评论则这条评论刚好在输入框上面,点击评论按钮则输入框刚好挡住按钮
int span = btnComment.getId() == R.id.btn_input_comment ? 0 : btnComment.getHeight();
listView.smoothScrollBy(coord[1] + span - inputViewCoordinatesInScreen[1], 1000);
}
}
@Override
public void onDismiss() {
}
});
}
public static class InputCommentListener {
// 评论成功时调用
public void onCommitComment() {
}
}
/**
* 弹出评论对话框
*/
private static Dialog showInputComment(Activity activity, CharSequence hint, final CommentDialogListener listener) {
final Dialog dialog = new Dialog(activity, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.view_input_comment);
dialog.findViewById(R.id.input_comment_dialog_container).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
if (listener != null) {
listener.onDismiss();
}
}
});
final EditText input = (EditText) dialog.findViewById(R.id.input_comment);
input.setHint(hint);
final TextView btn = (TextView) dialog.findViewById(R.id.btn_publish_comment);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener != null) {
listener.onClickPublish(dialog, input, btn);
}
}
});
dialog.setCancelable(true);
dialog.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (listener != null) {
int[] coord = new int[2];
dialog.findViewById(R.id.input_comment_container).getLocationOnScreen(coord);
// 传入 输入框距离屏幕顶部(不包括状态栏)的位置
listener.onShow(coord);
}
}
}, 300);
return dialog;
}
/**
* 评论对话框相关监听
*/
public interface CommentDialogListener {
void onClickPublish(Dialog dialog, EditText input, TextView btn);
/**
* onShow在输入法弹出后才调用
* @param inputViewCoordinatesOnScreen 输入框距离屏幕顶部(不包括状态栏)的位置[left,top]
*/
void onShow(int[] inputViewCoordinatesOnScreen);
void onDismiss();
}
}