ListView:item ClickableSpan自动删除Spannable Text

时间:2017-04-26 09:18:28

标签: android listview

我正在尝试将可扩展的TextView放在ListView项目中,这是第一次显示正常,但是当我滚动时,Spannable文本被删除。请帮我解决这个问题。

这是适配器代码

 ViewHolder viewHolder;

    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = mLayoutInflater.inflate(R.layout.row_drilldown_detail, null);

        viewHolder.rl_drill_down = (RelativeLayout) convertView.findViewById(R.id.rl_drill_down);
        viewHolder.ctvGroupTitle = (TextView) convertView.findViewById(R.id.ctv_group_title);
        viewHolder.ctv_interval_count = (TextView) convertView.findViewById(R.id.ctv_interval_count);
        viewHolder.ctv_interval_description = (TextView) convertView.findViewById(R.id.ctv_interval_description);
        viewHolder.ctv_coach_name = (TextView) convertView.findViewById(R.id.ctv_coach_name);
        viewHolder.ratingBar = (RatingBar) convertView.findViewById(R.id.rating_bar_group);

        convertView.setTag(viewHolder);

    }

    viewHolder = (ViewHolder) convertView.getTag();
if(!model.getDescription().isEmpty()) {
        viewHolder.ctv_interval_description.setVisibility(View.VISIBLE);
        viewHolder.ctv_interval_description.setText("Description: " + model.getDescription());
        AppUtils.makeTextViewResizable(viewHolder.ctv_interval_description, 3, "View More", true);
        viewHolder.ctv_interval_description.setFocusable(false);
        viewHolder.ctv_interval_description.setFocusableInTouchMode(false);
    } else
        viewHolder.ctv_interval_description.setVisibility(View.GONE);

这是Spannable Text Code

 public static void makeTextViewResizable(final TextView tv, final int maxLine,
                                         final String expandText, final boolean viewMore) {

    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);
            if (maxLine == 0) {
                int lineEndIndex = tv.getLayout().getLineEnd(0);
                String text = tv.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;
                tv.setText(text);
                tv.setMovementMethod(LinkMovementMethod.getInstance());
                tv.setText(
                        addClickablePartTextViewResizable(Html.fromHtml(tv.getText().toString()), tv, maxLine, expandText,
                                viewMore), TextView.BufferType.SPANNABLE);
            } else if (maxLine > 0 && tv.getLineCount() >= maxLine) {
                int lineEndIndex = tv.getLayout().getLineEnd(maxLine - 1);
                String text = tv.getText().subSequence(0, lineEndIndex - expandText.length() + 1) + " " + expandText;
                tv.setText(text);
                tv.setMovementMethod(LinkMovementMethod.getInstance());
                tv.setText(
                        addClickablePartTextViewResizable(Html.fromHtml(tv.getText().toString()), tv, maxLine, expandText,
                                viewMore), TextView.BufferType.SPANNABLE);
            } else {
                int lineEndIndex = tv.getLayout().getLineEnd(tv.getLayout().getLineCount() - 1);
                String text = tv.getText().subSequence(0, lineEndIndex) + " " + expandText;
                tv.setText(text);
                tv.setMovementMethod(LinkMovementMethod.getInstance());
                tv.setText(
                        addClickablePartTextViewResizable(Html.fromHtml(tv.getText().toString()), tv, lineEndIndex, expandText,
                                viewMore), TextView.BufferType.SPANNABLE);
            }
        }
    });

}

private static SpannableStringBuilder addClickablePartTextViewResizable(final Spanned strSpanned, final TextView tv,
                                                                        final int maxLine, final String spanableText, final boolean viewMore) {
    String str = strSpanned.toString();
    SpannableStringBuilder ssb = new SpannableStringBuilder(strSpanned);

    if (str.contains(spanableText)) {
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {

                if (viewMore) {
                    tv.setLayoutParams(tv.getLayoutParams());
                    tv.setText(tv.getTag().toString(), TextView.BufferType.SPANNABLE);
                    tv.invalidate();
                    makeTextViewResizable(tv, -1, "View Less", false);
                } else {
                    tv.setLayoutParams(tv.getLayoutParams());
                    tv.setText(tv.getTag().toString(), TextView.BufferType.SPANNABLE);
                    tv.invalidate();
                    makeTextViewResizable(tv, 3, "View More", true);
                }

            }
        }, str.indexOf(spanableText), str.indexOf(spanableText) + spanableText.length(), 0);

    }
    return ssb;

}

0 个答案:

没有答案