设置自定义字体我通过扩展CutomTextView
创建了TextView
,但在使用RecyclerView
时滚动时CutomTextView
滞后。我尝试使用标准Textview
并在ViewHolder
中设置字体,但结果相同。
CustomTextView.java
public class CustomTextView extends TextView {
public CustomTextView(Context context) {
super(context);
applyCustomFont(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyCustomFont(context);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
applyCustomFont(context);
}
private void applyCustomFont(Context context) {
setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/hell.ttf"));
}
}
Adapter.java
public class DashBoardAdapter extends RecyclerView.Adapter<DashBoardAdapter.ViewHolder> {
private List<PatientList> patientListList;
private Activity activity;
public DashBoardAdapter(List<PatientList> patientListList, Activity activity) {
this.patientListList = patientListList;
this.activity = activity;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(activity).inflate(R.layout.recyclerview_dashboard_row, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final PatientList p = patientListList.get(position);
if(p.getName() != null)
holder.username.setText(p.getName());
}
@Override
public int getItemCount() {
return patientListList.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
@BindView(R.id.patient_username) TextView username;
private View itemView;
public ViewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
ButterKnife.bind(this,itemView);
// Setting font here when custom textview is not used
}
}
@Override
public int getItemViewType(int position) {
return position;
}
可能是滞后的原因?
答案 0 :(得分:2)
看看这个链接,这可能会有所帮助 How to set a particular font for a button text in android?
每次使用字体时,您需要缓存字体并不需要初始化。