0
我知道關於回收列表中的行以獲得性能的方法。我通常看到的是,使用static
類和標籤(viewHolder
)Android中兩種回收列表行視圖之間的區別
例如一種技術:
@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
類和標籤更簡單的辦法,它只是檢查視圖是否回收,如果是的話,它使用它,如果沒有它創建它。
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());
}
它們之間有什麼不同,哪些更好用?