Using the ViewHolder Pattern
why view holder?
in bind view we always need to find the view by id ,than put the data in it.it is slow.
Because in the adapter,the view will be reuse.
so, we can save the reference of all the iteam, than reuse them later,no need to always call findviewbyid.
we use View Holder to save . A viewHolder responisble to save all the iteam of a specific view.
example of ViewHolder:
/**
| |
* Cache of the children views for a forecast list item.
| |
*/
| |
public static class ViewHolder {
| |
public final ImageView iconView;
| |
public final TextView dateView;
| |
public final TextView descriptionView;
| |
public final TextView highTempView;
| |
public final TextView lowTempView;
| |
public ViewHolder(View view) {
| |
iconView = (ImageView) view.findViewById(R.id.list_item_icon);
| |
dateView = (TextView) view.findViewById(R.id.list_item_date_textview);
| |
descriptionView = (TextView) view.findViewById(R.id.list_item_forecast_textview);
| |
highTempView = (TextView) view.findViewById(R.id.list_item_high_textview);
| |
lowTempView = (TextView) view.findViewById(R.id.list_item_low_textview);
| |
}
| |
}
|
So, we use get tag and settag to match a view and viewholder !!!
First.at new View,associte a ViewHolder with a view
@Override public View newView(Context context, Cursor cursor, ViewGroup parent) { // Choose the layout type int viewType = getItemViewType(cursor.getPosition()); int layoutId = -1; // TODO: Determine layoutId from viewType if(viewType==VIEW_TYPE_FUTUREDAY){ layoutId=R.layout.list_item_forecast; }else{ layoutId=R.layout.list_item_forecast_today; } View view=LayoutInflater.from(context).inflate(layoutId, parent, false); view.setTag(new ViewHolder(view)); return view; }
do by setTag
Sencond.get the viewholder at bindView
public void bindView(View view, Context context, Cursor cursor) {
// our view is pretty simple here --- just a text view
// we'll keep the UI functional with a simple (and slow!) binding.
// Read weather icon ID from cursor
int weatherId = cursor.getInt(ForecastFragment.COL_WEATHER_ID);
// Use placeholder image for now
ViewHolder vh=(ViewHolder)view.getTag();
ImageView iconView = vh.iconView;
iconView.setImageResource(R.drawable.ic_launcher);
//Read date from cursor
Long date=cursor.getLong(ForecastFragment.COL_WEATHER_DATE);
TextView dateview= vh.dateView;
dateview.setText(Utility.getFriendlyDayString(context,date));
// Read weather forecast from cursor
String w_forecast=cursor.getString(ForecastFragment.COL_WEATHER_DESC);
TextView forecastView=vh.descriptionView;
forecastView.setText(w_forecast);
// Read user preference for metric or imperial temperature units
boolean isMetric = Utility.isMetric(context);
// Read high temperature from cursor
double high = cursor.getDouble(ForecastFragment.COL_WEATHER_MAX_TEMP);
TextView highView = vh.highTempView;
highView.setText(Utility.formatTemperature(high, isMetric));
// Read low temperature from cursor
double low = cursor.getDouble(ForecastFragment.COL_WEATHER_MIN_TEMP);
TextView lowView =vh.highTempView
lowView.setText(Utility.formatTemperature(low, isMetric));
}
done by ViewHolder vh=(ViewHolder)view.getTag();