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. Any body has android VEN_18D1?

Just wandering if any one has android phone VEN_18D1

Manufacturer :  Google, Inc.
Product :       Android Sooner
Info :  53531309d1035cc9
USB Version :   2.00
Product ID :    VEN_18D1,DEV_DEED,PRT_00
Class : Vendor Specific
Max. Packet Size :      64 bytes
Max. Power :    256 mA
Open Pipes :    6

Thanks,
John

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

2. How to show a dialog on top of another dialog in an activity

Hi guys!

I have an activity that shows a dialog that has some editText fields
the user is supposed to fill in.  I would like to validate if the user
entered information in some fields, and in case he didn't, I would
like to show a dialog with an appropiate message on top of the
aforementioned dialog.  However I haven't been able to do so, because
once I write code on the onClick(DialogInterface dialog, int
whichButton) of the first dialog, this dialog closes and doesn't stay
in the screen.
Am I missing something?

Here's what I have:
protected Dialog onCreateDialog(int id) {
    return new AlertDialog.Builder(RegisterEvent.this)
            .setIcon(R.drawable.ico_registerevent_customization)
            .setTitle(R.string.tv_x_customization)
            .setView(textEntryView)
            .setPositiveButton(R.string.tv_x_ok, new
DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int
whichButton) {
                        if (!validInformation()) // Validates if the
user entered or not information
                                //Here's where I would like to show another 
dialog!!
                  }
     }).create();
Best regards,

Juan.
--~--~---------~--~----~------------~-------~--~----~

3. UEventObserver

4. Security library error

5. Research on Android Mobile Platform

6. shared Jar files between different project

7. Launch / Re-Launch flow