Using the Holder Pattern
Another somewhat expensive operation we do a lot with fancy views is call . 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 , change the icon in an ). Since can find widgets anywhere in the tree of children of the row’s root , 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 , like our rows, be declared totally in program code (in this case, Java). Then accessing individual widgets is merely a matter of calling a getter or accessing a field. And you can certainly do that with Android, but the code gets rather verbose. We need a way that lets us use the layout XML yet cache our row’s key child widgets so we have to find them only once. That’s where the holder pattern comes into play, in a class we’ll call .
All objects have and methods. These allow you to associate an arbitrary object with the widget. That holder pattern uses that “tag” to hold an object that, in turn, holds each of the child widgets of interest. By attaching that holder to the row , every time we use the row, we already have access to the child widgets we care about, without having to call again.
So, let’s take a look at one of these holder classes (taken from the sample project at http://apress.com/):
Class
Null
Null
Дата добавления: 2015-05-16; просмотров: 786;