vendredi 6 février 2015

Difference between two techniques recycling list row views in Android


Vote count:

0




I know the approach about recycling rows in list for the performance. What I see usually is the kind of technique that use static class and tags (viewHolder)


for example:



@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder = null;

if ( convertView == null )
{
convertView = mInflater.inflate(R.id.row, null);
holder = new ViewHolder();
holder.txt1 = (TextView) convertView.findViewById( R.id.txt1 );
holder.txt2 = (TextView) convertView.findViewById( R.id.txt2 );
holder.txt3 = (TextView) convertView.findViewById( R.id.txt3 );
// setting more images,images
convertView.setTag (holder);
}
else
{
holder = (ViewHolder) convertView.getTag ();
}

holder.txt1.setText( data.get( position ).txt1 );
holder.txt2.setText( data.get( position ).txt2 );
holder.txt3.setText( data.get( position ).txt3 );

return convertView;
}

static class ViewHolder{
TextView txt1;
TextView txt2;
TextView txt3;
}


but in some code I saw more simple approach that doesn't use static class and tags, it just check if the view is recycled if yes it uses it if not it create it.



public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(getActivity()).inflate(R.layout.history_row, null);
}
LinearLayout row = (LinearLayout) convertView.findViewById(R.id.row);

TextView txt1 = (TextView) convertView.findViewById(R.id.txt1);
txt1.setText(data.getTxt1());
TextView txt2 = (TextView) convertView.findViewById(R.id.txt1);
txt2.setText(data.getTxt2());
TextView txt3 = (TextView) convertView.findViewById(R.id.txt2);
txt3.setText(data.getTxt3());

}


What the different between them, and what is more better to use?



asked 30 secs ago







Difference between two techniques recycling list row views in Android

Aucun commentaire:

Enregistrer un commentaire