Search Engine

Saturday, May 22, 2010

Fancy ListViews, Part Three

In our last episode, we saw how we could save processing time — and, hence, battery life — by recycling existing row views in our fancy lists, simply by checking and reusing the convertView parameter passed into our getView().

In his comment to this series’ initial post, Romain Guy also left a bit of code that uses something called ViewHolder. Today, let’s take a look at this technique, with a twist.

The goal, once again, is to reduce the amount of work it takes to render one of our fancy lists, both so users get snappier response and so we don’t drain the battery so quickly. Recycling views saves us constructing new rows from whole cloth, particularly important when we are using ViewInflate and creating them from an XML layout.

Another somewhat expensive operation we do a lot with fancy views is call findViewById(). This dives into our inflated row and pulls out widgets by their assigned identifiers, so we can customize the widget contents (e.g., change the text of a TextView, change the icon in an ImageView). Since findViewById() can find widgets anywhere in the tree of children of the row’s root View, this could take a fair number of instructions to execute, particularly if we keep having to re-find widgets we had found once before.

In some GUI toolkits, this problem is avoided by having the composite Views, like our rows, be declared totally in program code (in this case, Java). Then, accessing individual widgets is merely the matter of calling a getter or accessing a field. And you can certainly do that with Android, but the code gets rather verbose. What would be nice is a way where we can still use the layout XML yet cache our row’s key child widgets so we only have to find them once.

That’s where ViewHolder comes in…or, in this post, a variation I’m calling ViewWrapper.

All View objects have getTag() and setTag() methods. These allow you to associate an arbitrary object with the widget. What the holder pattern does is use that “tag” to hold an object that, in turn, holds each of the child widgets of interest. By attaching that holder to the row View, every time we use the row, we already have access to the child widgets we care about, without having to call findViewById() again.

So, let’s take a look at one of these holder classes:

  1. class ViewWrapper {
  2. View base;
  3. TextView label=null;
  4. ImageView icon=null;
  5. ViewWrapper(View base) {
  6. this.base=base;
  7. }
  8. TextView getLabel() {
  9. if (label==null) {
  10. label=(TextView)base.findViewById(;
  11. }
  12. return(label);
  13. }
  14. ImageView getIcon() {
  15. if (icon==null) {
  16. icon=(ImageView)base.findViewById(;
  17. }
  18. return(icon);
  19. }
  20. }

The main difference between ViewWrapper (shown above) and ViewHolder (shown in the comment to this post) is that ViewWrapper lazy-finds the child widgets. If you create a wrapper and never need a specific child, you never go through the findViewById() operation for it and never have to pay for those CPU cycles. On the flip side, ViewHolder has the child widgets in public fields, whereas ViewWrapper uses getter methods, so there are going to be situations where a ViewHolder approach is more efficient.

The holder pattern also:

  • Allows us to consolidate all our per-widget type casting in one place, rather than having to cast it everywhere we call findViewById()

  • Perhaps track other information about the row, such as state information we are not yet ready to “flush” to the underlying model — more on this in an upcoming post

Using ViewWrapper is a matter of creating an instance whenever we inflate a row and attaching said instance to the row View via setTag(), as shown in this rewrite of last post’s demo’s getView() method:

  1. public View getView(int position, View convertView, ViewGroup parent) {
  2. View row=convertView;
  3. ViewWrapper wrapper=null;
  4. if (row==null) {
  5. ViewInflate inflater=context.getViewInflate();
  6. row=inflater.inflate(R.layout.row, null, null);
  7. wrapper=new ViewWrapper(row);
  8. row.setTag(wrapper);
  9. }
  10. else {
  11. wrapper=(ViewWrapper)row.getTag();
  12. }
  13. wrapper.getLabel().setText(getModel(position));
  14. if (getModel(position).length()>4) {
  15. wrapper.getIcon().setImageResource(R.drawable.delete);
  16. }
  17. return(row);
  18. }

Just as we check convertView to see if it is null in order to create the row Views as needed, we also pull out (or create) the corresponding row’s ViewWrapper. Then, accessing the child widgets is merely a matter of calling their associated methods on the wrapper.

Many thanks to Romain Guy for pointing out this technique!

In our next episode, we’ll look at putting interactive widgets in a row, not just static ones like TextView and ImageView, and discuss making modifications to the state that supplies the data for the list itself.


Post a Comment