0

To set a custom font I have created a CutomTextView by extending TextView but the RecyclerView lags while scrolling when the CutomTextView is used. I have tried using a standard Textview and setting the font in the ViewHolder but the result is the same.

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;
}

what could be the reason for the lag?

the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
Rajesh Gosemath
  • 1,812
  • 1
  • 17
  • 31

1 Answers1

2

Look at this link this might help How to set a particular font for a button text in android?

You need to cache Typeface doesn't need to initialize every time you use Typeface.

Community
  • 1
  • 1
Piyush Patel
  • 371
  • 1
  • 5
  • 13