I have a ListView with different layouts for different items. Some items are separators. Some items are different because they hold different kinds of data, etc.
I want to implement ViewHolders to speed up the getView process, but I'm not quite sure how to go about it. Different layouts have different pieces of data (which makes naming difficult) and different numbers of Views I want to use.
How should I go about doing this?
The best idea I can come up with is to create a generic ViewHolder with X items where X is the number of Views in an item layout with the highest number of them. For the other views with a small number of Views, I'll just use a subsection of those variables in the ViewHolder. So say I have 2 layouts I use for 2 different items. One has 3 TextViews and the other has 1. I would create a ViewHolder with 3 TextView variables and only use 1 of them for my other item. My problem is that this can get really ugly looking and feels really hacky; especially when an item layout may have many Views of many different types.
Here is a very basic getView:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyHolder holder;
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.layout_mylistlist_item, parent, false);
holder = new MyHolder();
holder.text = (TextView) v.findViewById(R.id.mylist_itemname);
v.setTag(holder);
}
else {
holder = (MyHolder)v.getTag();
}
MyListItem myItem = m_items.get(position);
// set up the list item
if (myItem != null) {
// set item text
if (holder.text != null) {
holder.text.setText(myItem.getItemName());
}
}
// return the created view
return v;
}
Suppose I had different types of row layouts, I could have a ViewHolder for each type of row. But what type would I declare "holder" to be at the top? Or would I declare a holder for each type and then use the one for the type of row I'm on.