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. Need assistance with a post

On Friday, I posted the question below.  My username (nickname) is
Marke2112.  As of this afternoon, I cannot find the posting.  I am
attempting to locate the posting by searching for keywords in my
posting.

Also, the email options for Google groups does not seem to have an
option for only receiving emails for my particular postings.  Do I
really have to receive emails or summary emails for all activity in
order to receive ones for my postings?

Thank you in advance.

Mark




-----Original Message-----
From: Marke2112 [mailto:ellen...@mlkerns.com] 
Sent: Friday, April 01, 2011 4:38 PM
To: Android Developers
Subject: Layout Buttons have no gradient on Droid X

I developed a simple app on my Droid (os v2.2.2).  The homepage has 4
buttons on it that display the default gradient gray background.
Everything looks great.  When I install the app on any of our in-
office Motorola Droid X (os v2.2 and v2.2.1) phones, the buttons are
dark with no gradient.

I've combed the forums, created a new hello world default project and
added a single button to confirm that it wasn't something goofy I did
in my app, and even tried manually setting the background color of the
button (see below).

v.getBackground().setColorFilter(Color.parseColor("#DDDDDD"),
PorterDuff.Mode.DST);

Everything looks great on the Droid, all buttons look dark and lack
gradient on the Droid X.

What am I missing???

Mark

-- 

2. OpenGL ES 2.0 Shader Access Client Memory

>From what I understand, the GPU in Android devices uses shared memory-
client memory directly.

The ideal would be to be able to pass an array pointer to the shader
and be able to access a value in that array using that pointer, in the
shader. If this is not possible I would still like to know the best
way of doing something like this.

(I also have another small question: would it be possible to draw a
quad from only 4 vertices in OpenGL ES 2.0?)

-- 

3. Widget development to show count

4. On Single back key press soft keyboard should be hidden and activity should be finished as well, how to make it possible?

5. InputMethod overlayed the edit area in webview when the hardware accelerate is enabled!

6. [WTA] YM di Galaxy 551

7. open a new activity in tab