最多显示6行并且最多显示三条文本

本文介绍了一个关于评论展示的逻辑处理方法,包括如何根据评论数量和屏幕宽度调整显示的评论条数,以及如何处理多行评论的显示问题。此外,还涉及了如何格式化评论内容,如颜色设置和昵称显示等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值