在文本末尾显示“更多”

时间:2019-02-08 13:32:19

标签: java android textview solution expandable

我想在几行文字之后添加“阅读更多”功能。我已提及该解决方案提供商here

下面是我的代码的结果:

点击“更多”之前:

Before Read More Is Clicked

点击“阅读更多”后:

After Read More Is Clicked

出现两个问题:
1.我想将最大行数设置为5行,但是“阅读更多”文本降至第6行。
2.在单击“阅读更多”按钮之前,第一行以“ xeo”结束,但是在单击“阅读更多”按钮之后,第一行以“ xeoh”结束。

下面是我的代码:

    /**
 * Make Text View Resizable
 * @param maxLine Max Line
 * @param expandText Expand Text
 * @param viewMore Is View More
 */
private void makeTextViewResizable(final int maxLine, final String expandText, final boolean viewMore) {
    if (mTextMessageContentText.getTag() == null) {
        mTextMessageContentText.setTag(mTextMessageContentText.getText());
    }

    ViewTreeObserver vto = mTextMessageContentText.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @SuppressWarnings("deprecation")
        @Override
        public void onGlobalLayout() {
            ViewTreeObserver obs = mTextMessageContentText.getViewTreeObserver();
            obs.removeGlobalOnLayoutListener(this);
            if (maxLine == 0) {
                int lineEndIndex = mTextMessageContentText.getLayout().getLineEnd(0);
                String text = mTextMessageContentText.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;
                mTextMessageContentText.setText(text);
                mTextMessageContentText.setMovementMethod(LinkMovementMethod.getInstance());
                mTextMessageContentText.setText(
                        addClickablePartTextViewResizable(mTextMessageContentText.getText().toString(), maxLine, expandText,
                                viewMore), TextView.BufferType.SPANNABLE);
            } else if (maxLine > 0 && mTextViewLineCount >= maxLine) {
                int lineEndIndex = mTextMessageContentText.getLayout().getLineEnd(maxLine - 1);
                String text = mTextMessageContentText.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;
                mTextMessageContentText.setText(text);
                mTextMessageContentText.setMovementMethod(LinkMovementMethod.getInstance());
                mTextMessageContentText.setText(addClickablePartTextViewResizable(mTextMessageContentText.getText().toString(), maxLine, expandText,
                        viewMore), TextView.BufferType.SPANNABLE);
            } else {
                int lineEndIndex = mTextMessageContentText.getLayout().getLineEnd(mTextViewLineCount - 1);
                String text = mTextMessageContentText.getText().subSequence(0, lineEndIndex) + " " + expandText;
                mTextMessageContentText.setText(text);
                mTextMessageContentText.setMovementMethod(LinkMovementMethod.getInstance());
                mTextMessageContentText.setText(
                        addClickablePartTextViewResizable(mTextMessageContentText.getText().toString(), lineEndIndex, expandText,
                                viewMore), TextView.BufferType.SPANNABLE);
            }
        }
    });
}

/**
 * Add Clickable Part Text View Resizable
 * @param stringSpanned String Spanned
 * @param maxLine Maximium Line
 * @param spannableText Spannable Text
 * @param viewMore Is View More
 * @return Sapnnable String Builder
 */
private SpannableStringBuilder addClickablePartTextViewResizable(final String stringSpanned, final int maxLine, final String spannableText, final boolean viewMore) {
    String str = stringSpanned.toString();
    SpannableStringBuilder ssb = new SpannableStringBuilder(stringSpanned);

    if (str.contains(spannableText)) {
        ssb.setSpan(new TextViewClickableSpan(ActivityChatMessageInfo.this, false, R.color.primary_text_color){
            @Override
            public void onClick(View widget) {
                if (viewMore) {
                    mTextMessageContentText.setLayoutParams(mTextMessageContentText.getLayoutParams());
                    mTextMessageContentText.setText(mTextMessageContentText.getTag().toString(), TextView.BufferType.SPANNABLE);
                    mTextMessageContentText.invalidate();
                    makeTextViewResizable(-1, "", false);
                } else {
                    mTextMessageContentText.setLayoutParams(mTextMessageContentText.getLayoutParams());
                    mTextMessageContentText.setText(mTextMessageContentText.getTag().toString(), TextView.BufferType.SPANNABLE);
                    mTextMessageContentText.invalidate();
                    makeTextViewResizable(maxLine, getString(R.string.caption_chat_media_slider_read_more), true);
                }
            }
        }, str.indexOf(spannableText), str.indexOf(spannableText) + spannableText.length(), 0);

    }
    return ssb;
}

请咨询。

谢谢。

0 个答案:

没有答案