Home > Tricks & Tips > Use View.tag mechanism for caching view

Use View.tag mechanism for caching view


This is an interesting caching alternative to ViewHolder pattern.

protected Object getViewHandle(View aParentView, int aViewToFind) {
	Object v = aParentView.getTag(aViewToFind);
	if (v==null) {
		v = aParentView.findViewById(aViewToFind);
		aParentView.setTag(aViewToFind,v);
	}
	return v;
}

The sample usage:

TextView tv = (TextView)getViewHandle(itemVi<wbr>ew,R.id.some_text_view);

This code is written by: Uncle Code Monkey at Android ViewHolder discussion

The original post from his blog.

 

Cheers,

Pete Houston

Categories: Tricks & Tips Tags: , , , ,
  1. rajasehkar
    February 4, 2014 at 3:14 pm

    Can you please explain it with examples … given links are not working 😦

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: