以编程方式设置 TextView 颜色的最佳性能
Best performance for setting TextView color programmatically
我有一个显示卡片视图的 RecyclerView。每个 CardView 有五 (5) 个 textView。根据每张卡片的类型,我将文本颜色更改为红色、蓝色或绿色(默认)。所有这些都发生在我的 onBindViewHolder 中,因此性能对我的用户体验至关重要。这是我现在在我的 onBindViewHolder 中所做的。
//set text color per card type
int int_textColor;
String type = arrayListFiltered.get(position).getType().toLowerCase();
String s_rating_type;
if (!FormatFactory.isStringEmpty(type)){
if (type.contains("featured")){
int_textColor = R.color.red;
}else if (type.contains("connector")){
int_textColor = R.color.blue;
}else{
int_textColor = R.color.green;
}
}else{
int_textColor =R.color.green;
}
setTextColor(recyclerViewHolder.tv_rating_type, int_textColor);
setTextColor(recyclerViewHolder.tv_length, int_textColor);
setTextColor(recyclerViewHolder.tv_name, int_textColor);
setTextColor(recyclerViewHolder.tv_location, int_textColor);
setTextColor(recyclerViewHolder.tv_summary, int_textColor);
这是我的 setTextColor 方法
private void setTextColor(TextView tv, int color){
tv.setTextColor(ContextCompat.getColor(tv.getContext(), color));
}
改变文本颜色的方法有很多种,我一直找不到任何关于提供最佳性能的方法的讨论。即使这段代码工作得很好,我还是会重写它以获得小的性能提升。
我认为你可以覆盖适配器的方法getItemViewType
,并用不同的颜色填充不同的布局。让适配器处理颜色变化,而不是动态改变颜色。
例如:
public class MyAdapter extends RecyclerView.Adapter<T> {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int rid;
switch (viewType) {
case 0: // for featured type
rid= R.layout.red;
break;
case 1 : // for connector type
rid= R.layout.blue;
break;
default:
rid= R.layout.green;
break;
}
View v= LayoutInflater.from(parent.getContext()).inflate(id, parent, false);
return new ViewHolder(v);
}
@Override
public int getItemViewType(int position) {
return arrayListFiltered.get(position).getType(); // need int type
}
...
}
我有一个显示卡片视图的 RecyclerView。每个 CardView 有五 (5) 个 textView。根据每张卡片的类型,我将文本颜色更改为红色、蓝色或绿色(默认)。所有这些都发生在我的 onBindViewHolder 中,因此性能对我的用户体验至关重要。这是我现在在我的 onBindViewHolder 中所做的。
//set text color per card type
int int_textColor;
String type = arrayListFiltered.get(position).getType().toLowerCase();
String s_rating_type;
if (!FormatFactory.isStringEmpty(type)){
if (type.contains("featured")){
int_textColor = R.color.red;
}else if (type.contains("connector")){
int_textColor = R.color.blue;
}else{
int_textColor = R.color.green;
}
}else{
int_textColor =R.color.green;
}
setTextColor(recyclerViewHolder.tv_rating_type, int_textColor);
setTextColor(recyclerViewHolder.tv_length, int_textColor);
setTextColor(recyclerViewHolder.tv_name, int_textColor);
setTextColor(recyclerViewHolder.tv_location, int_textColor);
setTextColor(recyclerViewHolder.tv_summary, int_textColor);
这是我的 setTextColor 方法
private void setTextColor(TextView tv, int color){
tv.setTextColor(ContextCompat.getColor(tv.getContext(), color));
}
改变文本颜色的方法有很多种,我一直找不到任何关于提供最佳性能的方法的讨论。即使这段代码工作得很好,我还是会重写它以获得小的性能提升。
我认为你可以覆盖适配器的方法getItemViewType
,并用不同的颜色填充不同的布局。让适配器处理颜色变化,而不是动态改变颜色。
例如:
public class MyAdapter extends RecyclerView.Adapter<T> {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
int rid;
switch (viewType) {
case 0: // for featured type
rid= R.layout.red;
break;
case 1 : // for connector type
rid= R.layout.blue;
break;
default:
rid= R.layout.green;
break;
}
View v= LayoutInflater.from(parent.getContext()).inflate(id, parent, false);
return new ViewHolder(v);
}
@Override
public int getItemViewType(int position) {
return arrayListFiltered.get(position).getType(); // need int type
}
...
}