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. What could we do with Android with this kind of investment...

Google doesn't have to worry. There's always the federal bailouts just
around the corner for failed companies!

take care,
Muthu Ramadoss.

http://linkedin.com/in/tellibitz +91-9840348914
http://androidrocks.in - Android Consulting.







--~--~---------~--~----~------------~-------~--~----~

2. FAILED Binder Transaction !

hi ,


    I create a Person class implements Parcelable in Android, The
Person include a byte array field.
When set the byte array size more than 40000, then find !!FAILED
Binder Transaction!!

Why the byte size so limit ?  How can I set the transaction size  in
Android?

Thanks
--~--~---------~--~----~------------~-------~--~----~

3. JNI run time error even I use same code and lib as Android source code.

4. How to customize Gallery Widget in android?

5. How to prevent my application being closed on clicking the back button on emulator

6. Learn Android Programming Online

7. I really need help with Android and I guess to some degree Java.