I have a ListView with different layouts for different elements. Some elements are delimiters. Some elements are different because they contain different types of data, etc.
I want to implement ViewHolders to speed up the getView process, but I'm not quite sure how to do this. Different layouts have different pieces of data (which makes naming difficult) and different numbers of views that I want to use.
How should I do it?
The best idea I can come up with is to create a common ViewHolder with X elements, where X is the number of views in the layout of the elements with the most of them. For other views with a small number of views, I just use the subsection of these variables in the ViewHolder. So to speak, I have 2 layouts that I use for two different items. One has 3 TextViews and the other has 1. I would create a ViewHolder with three TextView variables and use only one of them for my other element. My problem is that it can get really ugly and look really hacked; especially when the layout of an element can have many views for different types.
Here is a very simple 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 row type. But what type would I call a βholderβ on top? Or I can declare a holder for each type, and then use it for the type of string in which I am included.
android listview listactivity
Andrew Aug 18 '10 at 16:51 2010-08-18 16:51
source share