我有一個回收站視圖,想要更改某些部分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);
}
}
}
它不會改變顏色或文本大小。我究竟做錯了什麼??
@ WOLF.L對它有幫助 –
是的它的工作。謝謝@Sandeep –