ViewHolder paradigm vs. using View.setTag()

by Eric » Sun, 01 May 2011 12:27:59 GMT


Sponsored Links
 In the Google I/O 2010 - The world of ListView talk shown here:

 http://www.youtube.com/watch?v=wDBM6wVEO70& ;t=17m38s

a 'ViewHolder' paradigm is suggested to cache views and avoid the
findViewById lookup.  Could the same thing be achieved by using the
setTag(id, Object) method on View, and simply use the View's tag map
to store the reference by resource ID?  I think this would work, but
would it introduce any memory leak (or other) problems?


    public static final View getCachedViewByTagId(View parentView, int
viewToFindId) {
        Object object = parentView.getTag(viewToFindId);

        if(object == null) {
            object = parentView.findViewById(viewToFindId);

            if(object != null) {
                parentView.setTag(viewToFindId, object);
            }
        }

        return (View)object;
    }

-- 



Re: ViewHolder paradigm vs. using View.setTag()

by Romain Guy » Sun, 01 May 2011 12:48:32 GMT


 You could, but setTag(int, Object) forces you to use unique ids throughout
your app.







-- 
Romain Guy
Android framework engineer
romain...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them

-- 


Sponsored Links


Re: ViewHolder paradigm vs. using View.setTag()

by Mark Murphy » Sun, 01 May 2011 22:49:50 GMT


 


Well, now, that's a remarkably gentle response.

I seem to recall you being a bit more... forceful, shall we say, when
I suggested using the two-parameter setTag() to avoid having to fuss
with a holder class. Something about it wasn't there for app
developers but rather for libraries, or some such. As a result, I
destroyed that section of the book and salted the earth it sprang
from. :-)

Are the indexed getTag()/setTag() save for app developer consumption?
And, if so, what rules should we be following?

Thanks!

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://github.com/commonsguy 
 http://commonsware.com/blog  |  http://twitter.com/commonsguy 

_Android Programming Tutorials_ Version 3.4 Available!

-- 



Re: ViewHolder paradigm vs. using View.setTag()

by Eric » Mon, 02 May 2011 00:45:19 GMT


 


If you use the resource ID defined in the layout, that is always
unique by default, so that should work...?

-- 



Re: Re: ViewHolder paradigm vs. using View.setTag()

by Kostya Vasilyev » Mon, 02 May 2011 03:18:55 GMT


 01.05.2011 20:45, Eric :
If you use the resource ID defined in the layout, that is always unique by default, so that should work...?
It should, as using R.id.* values. However, if your item layout is relatively simple, I don't expect you'll get much of a speedup with a view holder vs. using itemLayoutRoot.findViewById. Have you done any profiling of your application? Anything interesting? -- Kostya Vasilyev -- http://kmansoft.wordpress.com --



Re: Re: ViewHolder paradigm vs. using View.setTag()

by Romain Guy » Mon, 02 May 2011 08:24:26 GMT


 >



It will depend on your phone. But even on a NexusOne and using list items 1
or 2 levels deep, the gain is around 4 or 5 fps. It's worth it.

-- 
Romain Guy
Android framework engineer
romain...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them

-- 



Other Threads

1. [WTA]Cara supaya Milestone membaca indikator batere hi-cap secara akurat

Dear RR,
Saya baru saja membeli batere high capacity untuk menyuport milestone saya.
Tapi indikator batre sepertinya blm bisa membaca dgn akurat (walau sdh low
bat d indikatornya, tapi masih bisa d pakai lama).
Bagaimana ya caranya spy indikator batere bisa akurat membaca batere saya?

-- 
===============

2. Good Book for Developing Android Testing App

Hello,

I think I have a handle on developing and android application, but
creating the unit test application
seems to be still be a problem for me.

Are their any good books or resources out their (besides the android
sdk website) that specifically discusses
developing the unit test side of the application?

Thanks

-- 

3. HelloTabwidget Application is not running

4. testFocusDistances Camera CTS Failure

5. Saving instance state of webview

6. clearTaskOnLaunch behavior ambiguous

7. Swapping fragments