使用相同的holder执行StringBuilder setText方法和ExpendableTextView setT



根据本文给出的答案,

Android-可扩展的文本视图与动画

我希望能够使用我的功能`

         holder.expandableTextView.setText(wallContent);
            SpannableStringBuilder ssb = SharefolioSpannableHandler.getSpannableStringBuilder(context, wallContent, currentWall.personTagged, currentWall.companyTagged);
            holder.expandableTextView.setMovementMethod(LinkMovementMethod.getInstance());
            holder.expandableTextView.setText(ssb);

`

这是SharefolioSpanableHandler类`

public class SharefolioSpannableHandler {
    public static SpannableStringBuilder getSpannableStringBuilder(final Context context, String text, List<Account> persons, List<Company> companies) {
        SpannableStringBuilder ssb = new SpannableStringBuilder(text);
        if (persons.size() != 0) {
            for (int i = 0; i < persons.size(); i++) {
                final Account person = persons.get(i);
                final String personName = persons.get(i).firstname;
                if (text.contains(personName)) {
                    int start = text.indexOf(personName) - 1;
                    int end = start + personName.length() + 1;
                    ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    ssb.setSpan(new ForegroundColorSpan(Color.BLUE), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    ssb.setSpan(new ClickableSpan() {
                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                             Intent intent = new Intent(context, LoadUserWallActivity.class);
                             intent.putExtra(LoadUserWallActivity.TAG, person);
                             context.startActivity(intent);
                        }
                    }, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        } 
        if (companies.size() != 0) {
            for (int i = 0 ; i < companies.size() ; i++) {
                final Company company = companies.get(i);
                final String companyCode = companies.get(i).companyCode;
                if (text.contains(companyCode)) {
                    int start = text.indexOf(companyCode) - 1;
                    int end = start + companyCode.length() + 1;
                    ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    ssb.setSpan(new ForegroundColorSpan(Color.BLUE), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    ssb.setSpan(new ClickableSpan() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context, CompanyActivity.class);
                            Bundle b = new Bundle();
                            b.putSerializable("company", company);
                            intent.putExtras(b);
                            context.startActivity(intent);
                        }
                    }, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return ssb;
    }
}
`

我目前有一些问题,因为作为持有者的ExpendableTextView已经有了setText,但如何包含StringBuilder函数给我带来了问题。如果我按照上面所示的方式运行,虽然它确实创建了Expandable TextView,但它并没有从链接或标记的角度来描述StringBuilder应该做什么。

有人知道如何将两个函数连接在一起吗?

谢谢Kingsley

我发现了我的问题。我只需要在ExpendableTextView中返回StringBuilder的输出,而不是这个

holder.expandableTextView.setText(wallContent);
            SpannableStringBuilder ssb = SharefolioSpannableHandler.getSpannableStringBuilder(context, wallContent, currentWall.personTagged, currentWall.companyTagged);
            holder.expandableTextView.setMovementMethod(LinkMovementMethod.getInstance());
            holder.expandableTextView.setText(ssb);

我做了这个

holder.expandableTextView.setText(wallContent);         
holder.expandableTextView.setMovementMethod(LinkMovementMethod.getInstance());
holder.expandableTextView.setText(SharefolioSpannableHandler.getSpannableStringBuilder(context, wallContent, currentWall.personTagged, currentWall.companyTagged));

非常适合我。

相关内容

  • 没有找到相关文章

最新更新