我尝试在文字视图上实施“显示更多”和“显示更少”功能,应该在点击“显示更多”或“显示更少文字”时展开和折叠。
问题在于,如果我在单击“显示更多”和“放大”时展开文本视图向下滚动列表,它再次崩溃。
感谢。
private void bindPostContent(ViewHolder holder, Cursor c) {
//Main text in HTML format
String postContent = c.getString(thoughtC);
holder.tvPostHtml.setVisibility(View.VISIBLE);
holder.tvPostHtml.setText(Html.fromHtml(c.getString(thoughtC)));
Linkify.addLinks(holder.tvPostHtml, Linkify.WEB_URLS);
if (!TextUtils.isEmpty(postContent)) {
if (postContent.length() > 240) {
resizePostContent(holder.tvPostHtml, "...Show More", true);
}
} else {
holder.tvPostHtml.setVisibility(View.GONE);
}
//Display attachments
AttachmentContainer container = new AttachmentContainer(tid, holder.llAttachmentContainer,
ThoughtContainerType.THOUGHT,
context, c.getString(attachmentC),
attachJsonMapper, pollState);
value = container.value;
container.renderAttachmentViews();
}
public void resizePostContent(final TextView tvPostContent, final String showMoreLessLabel, final boolean showMore) {
if (tvPostContent.getTag() == null) {
tvPostContent.setTag(tvPostContent.getText());
}
ViewTreeObserver vto = tvPostContent.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = tvPostContent.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
if (showMore) {
String text = tvPostContent.getText().subSequence(0, 240) + " " + showMoreLessLabel;
tvPostContent.setText(text);
tvPostContent.setMovementMethod(LinkMovementMethod.getInstance());
tvPostContent.setText(showMoreLess(Html.fromHtml(tvPostContent.getText().toString()), tvPostContent, showMoreLessLabel, showMore));
} else {
String text = tvPostContent.getText().subSequence(0, tvPostContent.getText().length()) + " " + showMoreLessLabel;
tvPostContent.setText(text);
tvPostContent.setMovementMethod(LinkMovementMethod.getInstance());
tvPostContent.setText(showMoreLess(Html.fromHtml(tvPostContent.getText().toString()), tvPostContent, showMoreLessLabel, showMore));
} tvPostContent.setOnClickListener(clickListener);
}
});
}
private SpannableStringBuilder showMoreLess(final Spanned postContentSpanned, final TextView tvPostContent, final String showMoreLessLabel, final boolean viewMore) {
String str = postContentSpanned.toString();
SpannableStringBuilder ssb = new SpannableStringBuilder(postContentSpanned);
if (str.contains(showMoreLessLabel)) {
ssb.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
tvPostContent.setOnClickListener(null);
if (viewMore) {
tvPostContent.setLayoutParams(tvPostContent.getLayoutParams());
tvPostContent.setText(tvPostContent.getTag().toString());
resizePostContent(tvPostContent, "Show Less", false);
} else {
tvPostContent.setLayoutParams(tvPostContent.getLayoutParams());
tvPostContent.setText(tvPostContent.getTag().toString());
resizePostContent(tvPostContent, "...Show More", true);
}
}
@Override public void updateDrawState(TextPaint ds){
ds.setUnderlineText(false);
ds.setColor(context.getResources().getColor(R.color.newsfeed_item_action_performed));
}
}, str.indexOf(showMoreLessLabel), str.indexOf(showMoreLessLabel) + showMoreLessLabel.length(), 0);
}
return ssb;
}