添加"查看更多"在textline结束后3行并在对话框中显示全文(弹出)

时间:2017-12-14 11:34:56

标签: android listview baseadapter

我想添加" ..更多"在textview的第3行末尾,如果我们点击更多,我们将在弹出窗口(或自定义对话框)中显示全文。所以我使用下面的代码来实现这个功能。当您处于纵向模式时,代码工作正常,但是当您更改设备方向时,它无法正常工作。我正在使用BaseAdapter和RecyclerView(使用ViewHelper设计模式)。

public interface ViewMoreClickListener {
  void viewMoreClicked(int index);
}

public void addViewMoreToTextView(final int position, final TextView tv, final String expandText, final int maxLine, final ViewMoreClickListener listener) {
    try {
        if (tv.getTag() == null) {
            tv.setTag(tv.getText());
        }

        ViewTreeObserver vto = tv.getViewTreeObserver();

        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onGlobalLayout() {
                ViewTreeObserver obs = tv.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);

                String text = tv.getText().toString();

                if (tv.getLineCount() > maxLine) {
                    int lineEndIndex = tv.getLayout().getLineEnd(maxLine - 1);
                    text = text.subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;
                    tv.setText(text);

                    // We are rechecking in order to resolve issue with alignment problem
                    if (tv.getLineCount() > maxLine) {
                        lineEndIndex = tv.getLayout().getLineEnd(maxLine - 1);
                        text = text.subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;
                        tv.setText(text);
                    }

                    tv.setMovementMethod(LinkMovementMethod.getInstance());

                    String str = Html.fromHtml(text).toString();
                    SpannableStringBuilder ssb = new SpannableStringBuilder(Html.fromHtml(text));

                    if (str.contains(expandText)) {
                        ssb.setSpan(new ClickableSpan() {
                            @Override
                            public void onClick(View widget) {
                                if (listener != null)
                                    listener.viewMoreClicked(position);
                            }
                        }, str.indexOf(expandText), str.indexOf(expandText) + expandText.length(), 0);
                    }

                    tv.setText(ssb);
                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public class SampleActivity extends BaseActivity implements ViewMoreClickListener {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
   }

   @Override
   public void viewMoreClicked(int index) {
       // here we will show full text in pop up or custom dialog by getting from arraylist based on index.
   }
}

我从适配器的getView()调用上面的方法。

addViewMoreToTextView(position, textview, "..more", 3, listener);

我已经彻底完成了这些链接,但它们也没有在RecyclerView中工作。

Add "View More" at the end of textview after 3 lines

Set Text View ellipsize and add view more at end

请帮我解决此问题。

1 个答案:

答案 0 :(得分:0)

我已使用以下代码解决了这个问题:

public void addViewMoreToTextView(final int position, final TextView textView, final String text, final String expandText, final int maxLine, final ViewMoreClickListener listener) {
    try {
        textView.post(new Runnable() {
            @Override
            public void run() {
                SpannableStringBuilder truncatedSpannableString;
                int startIndex;

                if (textView.getLineCount() > maxLine) {
                    int lastCharShown = textView.getLayout().getLineVisibleEnd(maxLine - 1);
                    String displayText = text.substring(0, lastCharShown - expandText.length() + 1) + " " + expandText;
                    startIndex = displayText.indexOf(expandText);

                    truncatedSpannableString = new SpannableStringBuilder(displayText);

                    textView.setText(truncatedSpannableString);

                    truncatedSpannableString.setSpan(new ClickableSpan() {
                        @Override
                        public void onClick(View widget) {
                            // this click event is not firing that's why we are adding click event for text view below.
                        }
                    }, startIndex, startIndex + expandText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                    textView.setText(truncatedSpannableString);

                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (listener != null)
                                listener.viewMoreClicked(position);
                        }
                    });

                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}