2017-03-08 95 views
1

我有一個回收站視圖,想要更改某些部分textview文本的顏色。Android Spannable不能在回收站視圖適配器中工作

這裏是我的代碼:

public class MyAdapter extends RecyclerView.Adapter<MyEarningsAdapter.ViewHolder> { 
    public ArrayList<WageItemDataSet> wageItemList; 
    private Context context; 
    private Spannable spanText; 
    private ForegroundColorSpan fcs; 
    private RelativeSizeSpan rss; 
    public MyAdapter(Context context) { 
     wageItemList = new ArrayList<>(); 
     this.context = context; 
     fcs = new ForegroundColorSpan(Color.BLACK); 
     rss = new RelativeSizeSpan(0.8f); 
    } 
@Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_wage_list, parent, false)); 
    } 
    @Override 
     public void onBindViewHolder(ViewHolder holder, int position) { 
      WageItemDataSet item = wageItemList.get(position); 
      spanText = new SpannableString("Fare: "+item.totalFare); 
      spanText.setSpan(fcs, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      spanText.setSpan(rss, 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      holder.tvFareAmount.setText(spanText); 

     } 

     public class ViewHolder extends RecyclerView.ViewHolder { 
      TextView tvFareAmount; 
      public ViewHolder(View itemView) { 
       super(itemView); 
       tvFareAmount = (TextView) itemView.findViewById(R.id.tvFareAmount); 

       tvFareAmount.setTypeface(openSansRegular); 

      } 
     } 
} 

它不會改變顏色或文本大小。我究竟做錯了什麼??

回答

0

您好我已經實現了同樣的事情,它的工作對我來說。你必須做一個類,如下擴展ClickableSpan喜歡它:在onBindViewHolder()方法

public class TextSpan extends ClickableSpan { 
     @Override 
     public void onClick(View widget) { 
     TextView tv = (TextView) widget; 
     Spanned s = (Spanned) tv.getText(); 
     //int start = s.getSpanStart(this); 
     //int end = s.getSpanEnd(this); 
    } 
    } 

,然後在你的,你必須編寫代碼如下

 SpannableStringBuilder builder = new SpannableStringBuilder(); 
    builder.append("yourstring"); 
    builder.setSpan(new TextSpan(), builder.length() -   
    comment.length(),builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    holder.tvFareAmount.setText(content); 
    holder.tvFareAmount.setMovementMethod(LinkMovementMethod.getInstance()); 

我希望這將解決你的問題

+0

@ WOLF.L對它有幫助 –

+0

是的它的工作。謝謝@Sandeep –

0

試試這個,

WageItemDataSet item = wageItemList.get(position); 
    String value="Fare: "+item.totalFare; 
    Spannable spanText = new SpannableString(value);   
    spanText.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 13, 
    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    textview.setText(spanText); 
+0

謝謝,但不工作 –

0

這爲我工作 -

StringBuilder captionBuilder = new StringBuilder(); 
captionBuilder.append(yourString); 
SpannableStringBuilder commentBuilder = new SpannableStringBuilder(captionBuilder); 
commentBuilder.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.your_color)), start, end, 0); 

希望它能幫助。下面

+0

我試着用Spannable,SpannableString,SpannableStringBuilder。沒有什麼工作 –

+0

好吧,我使用'StringBuilder'工作。它在我的應用程序中工作。去嘗試一下。 – Wizard

0

使用,以實現自己的目標......

public static void spanText(Context context, AppCompatAutoCompleteTextView editText, String replaceString) { 
    String event = editText.getHint().toString(); 
    int startPos = event.indexOf(replaceString); 
    int endPos = startPos + replaceString.length(); 

    Spannable spanText = Spannable.Factory.getInstance().newSpannable(editText.getHint()); 
    spanText.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.dark_yellow)), startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
    editText.setHint(spanText); 
} 
+0

謝謝,但沒有工作 –

0

嘗試.append(),而不是.settext()

holder.tvFareAmount.append(spanText) 
相關問題