در لیست ویوی اپلیکیشن من روی هر آیتم پردازش تقریبا سنگینی انجام میگیرد (مانند خواندن اطلاعات از یک دیتابیس یک مگابایتی و گرفتن خروجی از یک الگوریتم) و هنگامی که لیست ویو را به سمت بالا و پایین اسکرول میکنم کمی لگ و کندی در برنامه مشاهده میشود و گاهی باعث خارج شدن از برنامه میگردد. با جستجو در اینترنت هم متدی به نام ViewHolder پیدا کردم که گفته میشد در افزایش سرعت اسکرول موثر است بنابراین کدم را به شکل زیر تغییر دادم (در درستی کد تردید دارم)، اما همچنان کمی لگ و کندی مشاهده میشود:
کد پیاچپی:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.network_list_row, null);
holder = new ViewHolder();
holder.text1= (TextView)convertView.findViewById(R.id.txt1);
holder.text2 =(TextView)convertView.findViewById(R.id.txt2);
holder.text3 =(TextView)convertView.findViewById(R.id.txt3);
holder.text4 = (TextView)convertView.findViewById(R.id.txt4);
holder.text5 = (TextView)convertView.findViewById(R.id.txt5);
holder.imageView = (ImageView)convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
}else{
holder= (ViewHolder) convertView.getTag();
}
holder.text1.setText(convertView.getContext().getString(R.string.11_msg, myValue1));
holder.text2.setText(Integer.toString(myValue2));
holder.text3.setText(convertView.getContext().getString(R.string.22_msg, myValue3));
holder.text4.setText(convertView.getContext().getString(R.string.33_msg,myValue4));
holder.text5.setText(convertView.getContext().getString(R.string.44_msg,myValue5));
return convertView;
}
private static class ViewHolder {
public TextView text1;
public TextView text2;
public TextView text3;
public TextView text4;
public TextView text5;
public ImageView imageView;
}