package com.cattsoft.deliersystem.view;
import android.util.SparseArray;
import android.view.View;
/**
* listView优化工具类ViewHolder
*
* @author lenovo
*
*/
@SuppressWarnings("unchecked")
public class ViewHolder {
public static final <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;
}
import android.util.SparseArray;
import android.view.View;
/**
* listView优化工具类ViewHolder
*
* @author lenovo
*
*/
@SuppressWarnings("unchecked")
public class ViewHolder {
public static final <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;
}
}
在适配器中调用
@Override
public View getView(final int position, View view, ViewGroup parent) {
if (view == null) {
view = inflater.inflate(R.layout.item_caselist, null, false);
}
TextView item_caseno = ViewHolder.get(view, R.id.item_caseno);
ImageView item_caseImg = ViewHolder.get(view, R.id.item_caseImg);
return view;
}