private void setCommentContent(ViewHolder vh, String feedId, int commentNum, ArrayList<CommentItem> comment_lists)
{
if(commentNum <= 0 || comment_lists == null || comment_lists.isEmpty())
{
for(int i = 0; i < vh.tvCommentTexts.size(); i++)
{
View view = vh.tvCommentTexts.get(i);
view.setVisibility(View.GONE);
}
vh.llCommentContent.setVisibility(View.GONE);
vh.tvCommentOtherLine.setVisibility(View.GONE);
vh.tvCommentOther.setVisibility(View.GONE);
}
else
{
vh.llCommentContent.setVisibility(View.VISIBLE);
int realNum = Math.min(commentNum, comment_lists.size());
int count = Math.min(vh.tvCommentTexts.size(), realNum);
// float width = 0;
// int commentLine = 0;
// for(int j = 0; j < count; j++)
// {
// CommentItem comment = comment_lists.get(j);
// width = measureTextWidth(vh.tvCommentTexts.get(0), comment.getContent());
// int tLine = commentLine + (int)(width / mCommentWidth);
// if(width % mCommentWidth > 0) tLine = tLine + 1;
// if(tLine > 4)
// {
// count = j + 1;
// break;
// }
// commentLine = tLine;
// }
float width = 0;
int commentLine = 0;
int i = 0;
for(; i < count; i++)
{
TextView view = vh.tvCommentTexts.get(i);
view.setVisibility(View.VISIBLE);
CommentItem comment = comment_lists.get(i);
CharSequence commentContent = formatCommentContent(comment);
view.setText(commentContent);
width = measureTextWidth(view, commentContent.toString());
int tLine = commentLine + (int)(width / mCommentWidth);
if(width % mCommentWidth > 0) tLine = tLine + 1;
if(tLine >= 6 && (6 - commentLine > 0))
{
// view.setVisibility(View.GONE);
view.setMaxLines(6 - commentLine);
i++;
break;
}
else
{
view.setMaxLines(6);
}
commentLine = tLine;
}
if(i < commentNum)
{
int leftCount = commentNum - i;
vh.tvCommentOtherLine.setVisibility(View.VISIBLE);
vh.tvCommentOther.setVisibility(View.VISIBLE);
vh.tvCommentOther.setText(mResources.getString(R.string.bbs_comment_other, leftCount));
vh.tvCommentOther.setOnClickListener(new DynamicDetailClickListener(feedId, false));
}
else
{
vh.tvCommentOtherLine.setVisibility(View.GONE);
vh.tvCommentOther.setVisibility(View.GONE);
}
for(; i < vh.tvCommentTexts.size(); i++)
{
View view = vh.tvCommentTexts.get(i);
view.setVisibility(View.GONE);
}
}
}
private CharSequence formatCommentContent(CommentItem comment)
{
String nick = comment.getNick();
String replayNick = comment.getReply_nick();
if(mMyUid.equals(String.valueOf(comment.getUid())))
{
nick = getMyNick(nick);
}
if(mMyUid.equals(String.valueOf(comment.getReply_uid())))
{
replayNick = getMyNick(replayNick);
}
int color = (comment.getType() == 1 || comment.getType() == 2) ? redColor : blueColor;
int replyColor = (comment.getReply_type() == 1 || comment.getReply_type() == 2) ? redColor : blueColor;
SpannableStringBuilder spanText = null;
if(TextUtils.isEmpty(replayNick))
{
String prefix = nick + MAOHAO;
spanText = new SpannableStringBuilder(prefix + comment.getContent());
spanText.setSpan(new ForegroundColorSpan(color), 0, prefix.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else
{
String prefix1 = nick + HUIFU;
String prefix2 = prefix1 + replayNick+ MAOHAO;
spanText = new SpannableStringBuilder(prefix2 + comment.getContent());
spanText.setSpan(new ForegroundColorSpan(color), 0, nick.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spanText.setSpan(new ForegroundColorSpan(replyColor), prefix1.length(), prefix2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spanText;
}
private float measureTextWidth(TextView textView, String content)
{
Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(textView.getCurrentTextColor());
mTextPaint.setTextSize(textView.getTextSize());
float textWidth = mTextPaint.measureText(content);
return textWidth;
}
转载于:https://my.oschina.net/bruces/blog/597354