Apparently it wasn't clear that I was using my own implementation of a SimpleCursorAdapter
, @MarsAtomic and @android developer both suggested I override getView()
, however, when using a SimpleCursorAdapter
you don't, you override both onNewView()
and onBindView()
.
What I ended up doing, which is what I wanted to avoid was simply recreating the adapter from scratch, and setting the TextSize during onNewView()
. I am pretty pleased with the results, since that minimizes the amount of calls to textview.setTextSize(size)
by only setting the size on new views.
First Step, on my activity, check during onResume if font size changed, if it did, recreate the adapter from scratch:
final int oldSize = mCurFontSize;
mCurFontSize = Integer.valueOf(mPreferences.getString(getString(R.string.pref_key_font_size), "0"));
if (oldSize != mCurFontSize) {
//Only re-do the adapter if needed
Constants.logMessage("re-creating adapter");
mArticleAdapter = new CursorListAdapter(Home.this, R.layout.list_item,
mCursor, FROM, TO, 0, mCurFontSize);
mArticlesListView.setAdapter(mArticleAdapter);
}
}
Step two, on the adapter constructor, set the font size values to the instance variables.
public CursorListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags, int textSize) {
super(context, layout, c, from, to, flags);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRootLayout = layout;
mResources = context.getResources();
setAdjustTextSize(textSize);
}
Where setAdjustTextSize does this:
public void setAdjustTextSize(int size) {
mTextSize = size;
switch (size) {
case ArticleViewFragment.FONT_SIZE_SMALL:
mTitleTextSizeRes = R.dimen.title_size_small;
mCategoryTextSizeRes = R.dimen.description_size_small;
mDescripTextSizeRes = R.dimen.description_size_small;
break;
case ArticleViewFragment.FONT_SIZE_MEDIUM:
mTitleTextSizeRes = R.dimen.title_size_medium;
mCategoryTextSizeRes = R.dimen.description_size_medium;
mDescripTextSizeRes = R.dimen.description_size_medium;
break;
case ArticleViewFragment.FONT_SIZE_LARGE:
mTitleTextSizeRes = R.dimen.title_size_large;
mCategoryTextSizeRes = R.dimen.description_size_large;
mDescripTextSizeRes = R.dimen.description_size_large;
break;
case ArticleViewFragment.FONT_SIZE_EXTRA_LARGE:
mTitleTextSizeRes = R.dimen.title_size_extra_large;
mCategoryTextSizeRes = R.dimen.description_size_extra_large;
mDescripTextSizeRes = R.dimen.description_size_extra_large;
break;
default:
break;
}
}
Step Three: During onNewView()
set the text size.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View container = mInflater.inflate(mRootLayout, null);
ViewHolder mHolder = new ViewHolder();
mHolder.category = (TextView) container.findViewById(R.id.article_category);
mHolder.title = (TextView) container.findViewById(R.id.article_title);
mHolder.description = (TextView) container.findViewById(R.id.article_descrp);
mHolder.image = (ImageView) container.findViewById(R.id.article_image);
mHolder.category.setTextSize(TypedValue.COMPLEX_UNIT_PX, mResources.getDimension(mCategoryTextSizeRes));
mHolder.title.setTextSize(TypedValue.COMPLEX_UNIT_PX, mResources.getDimension(mTitleTextSizeRes));
mHolder.description.setTextSize(TypedValue.COMPLEX_UNIT_PX, mResources.getDimension(mDescripTextSizeRes));
container.setTag(mHolder);
return container;
}
And that is about it. It works, it doesn't call setTextSize many times during the adapter's life, I only recreate the adapter when the font size did change, and we are all happy.