Здесь вы используете стандартный шаблон для пользовательского ListAdapter
. Не все виды будут переработаны, например когда они впервые созданы для заполнения ListView
.
Вы также можете использовать ссылку LayoutInflater
при создании адаптера для небольшого повышения эффективности, см. Фрагмент ниже
private class AlertListAdapter extends ArrayAdapter< Alert >
{
private ViewHolder holder;
private LayoutInflater mInflater;
public AlertListAdapter( Context context, List< Alert > items )
{
super( context, R.layout.dashboard_layout, items );
mInflater = LayoutInflater.from( context );
}
public View getView( int position, View recycledView, ViewGroup parent )
{
if ( recycledView == null || recycledView.getTag() == null )
{
recycledView = mInflater.inflate( R.layout.list_item, null );
holder = new ViewHolder();
holder.header = ( LinearLayout ) recycledView.findViewById( R.id.alert_list_item_header );
holder.header_text = ( TextView ) recycledView.findViewById( R.id.alert_list_item_header_text );
holder.header_count = ( TextView ) recycledView.findViewById( R.id.alert_list_item_header_count );
holder.name = ( TextView ) recycledView.findViewById( R.id.alert_list_item_name );
holder.distance = ( TextView ) recycledView.findViewById( R.id.alert_list_item_distance );
recycledView.setTag( holder );
}
else
{
holder = ( ViewHolder ) recycledView.getTag();
}
holder.header_text.setText( title.substring( 0, space ) );
holder.name.setText( title.substring( space + 1 ) );
holder.header_count.setText( count );
holder.header.setBackgroundResource( resourceID );
return recycledView;
}
}
По сути, вы всегда должны быть готовы к тому, что v.getTag()
вернет значение NULL и, соответственно, увеличит значение View
.