Skip to content

Instantly share code, notes, and snippets.

@darkengine
Created December 15, 2015 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkengine/e3ebec2647bb055215ed to your computer and use it in GitHub Desktop.
Save darkengine/e3ebec2647bb055215ed to your computer and use it in GitHub Desktop.
Generic ViewHolder
package com.rockyapps.dbdemo.ui;
import android.util.SparseArray;
import android.view.View;
/**
* Created by apple on 15/12/15.
*/
public class GenericViewHolder {
@SuppressWarnings("unchecked")
public static <T extends View> T get(View view, int id) {
SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
if (viewHolder == null) {
viewHolder = new SparseArray<View>();
view.setTag(viewHolder);
}
View childView = viewHolder.get(id);
if (childView == null) {
childView = view.findViewById(id);
viewHolder.put(id, childView);
}
return (T) childView;
}
}
@darkengine
Copy link
Author

Android generic view holder, makes working with ListView and GridView conveniently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment