Reputation: 15734
I have a ListView
with a hundred items that scrolls fine. I added one custom font to one of the TextView
's and now it is choppy. I have tried both otf and ttf fonts.
Here is how I have code (simplified):
public class ItemAdapter extends ArrayAdapter<ItemObject> implements
SectionIndexer {
private Typeface myTypeface; // class variable
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_row_layout, parent,
false);
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.itemName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ItemObject io = getItem(position);
String name = io.name;
myTypeface = Typeface.createFromAsset(convertView.getContext().getAssets(), "fonts/listitemfont.ttf");
holder.t1.setText(name);
holder.t1.setTypeface(myTypeface);
return convertView;
}
Is there a better way?
Upvotes: 2
Views: 930
Reputation: 86958
You are creating the same typeface every time getView()
is called, move this line into your constructor:
myTypeface = Typeface.createFromAsset(convertView.getContext().getAssets(), "fonts/listitemfont.ttf");
And you probably don't need to re-set the typeface every time either, just when the TextView is created.
if (convertView == null) {
...
holder.t1 = (TextView) convertView.findViewById(R.id.itemName);
holder.t1.setTypeface(myTypeface); // Move this here
}
Upvotes: 8
Reputation: 8548
It appears that you're creating a new Typeface every time getView is called (which is a lot). Try moving myTypeface = Typeface.createFromAsset(convertView.getContext().getAssets(), "fonts/listitemfont.ttf");
into the constructor for your adapter.
Upvotes: 6