Android 中回收列表行视图的两种技术之间的区别
Difference between two techniques recycling list row views in Android
我知道回收列表中的行以提高性能的方法。我经常看到的是那种使用static
class和标签(viewHolder
)
的技术
例如:
@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;
}
但在一些代码中我看到了更简单的方法,它不使用 static
class 和标签,它只是检查视图是否被回收如果是它使用它如果不是它创建它.
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());
}
它们有什么区别,哪个更好用?
findViewById()
是一个昂贵的电话。人们应该避免使用它。在第一种方法中,findViewById()
是为所有新创建的视图而不是为任何 convertView 调用的。在第二种方法中,所有视图都调用 findViewById()
。第一种方式无疑更好用
我知道回收列表中的行以提高性能的方法。我经常看到的是那种使用static
class和标签(viewHolder
)
例如:
@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;
}
但在一些代码中我看到了更简单的方法,它不使用 static
class 和标签,它只是检查视图是否被回收如果是它使用它如果不是它创建它.
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());
}
它们有什么区别,哪个更好用?
findViewById()
是一个昂贵的电话。人们应该避免使用它。在第一种方法中,findViewById()
是为所有新创建的视图而不是为任何 convertView 调用的。在第二种方法中,所有视图都调用 findViewById()
。第一种方式无疑更好用