Style first 2 TextViews in Android ListView differently
- by Kurtis Nusbaum
I have a ListView and I want the first two entries in it to be displayed differently than the rest. Nothing fancy, I want them all to just be text views. But the first two entries need to have different sizes and weights than the rest. I tried modifying the ArrayAdapter class like so:
private class BusStopAdapter<T> extends ArrayAdapter<T>{
public BusStopAdapter(
Context context, int textViewResourceId,
List<T> objects)
{
super(context, textViewResourceId, objects);
}
public View getView(int position, View convertView,
ViewGroup parent)
{
TextView toReturn =
(TextView)super.getView(position, convertView, parent);
if(position == 0){
toReturn.setTextSize(12);
toReturn.setText("Previous Bus: " + toReturn.getText());
toReturn.setPadding(0,0,0,0);
}
else if(position == 1){
toReturn.setTextSize(20);
toReturn.setPadding(
toReturn.getPaddingLeft(),
0,
toReturn.getPaddingRight(),
0
);
toReturn.setText("Next Bus: " + toReturn.getText());
toReturn.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.TOP);
}
return toReturn;
}
}
But this inadvertantly causes some of the other textviews to take on these special attributes. I think it's because cause textviews get "recycled" in the AbsListAdapter class.