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

时间:2014-10-14 10:44:48

标签: android textview stringbuilder expandable

根据这篇文章给出的答案,

Android - Expandable TextView with Animation

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

         holder.expandableTextView.setText(wallContent);

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

`

这是SharefolioSpannableHandler类 `

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在链接或标记方面应该执行的操作。

有没有人知道如何将两个功能连接在一起?

由于 金斯利

1 个答案:

答案 0 :(得分:0)

我发现了我的问题。我只需要在我的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));

完美地为我工作。