I have a following performance problem. I am developing an android tablet app and there is a gridview like a gallery. Each item has about 3 textviews (short text) and 2 image views (1 for background and 1 small icon for important items). Target device resolution is quite big (1920x800) so grid matrix is 6 x 5 items on display.
I have implemented that but scrolling experience is bad. When I scroll slowly anytime the new line of 6 items should appear there is a short freeze. Then the line will come and scrolling is ok until the end of the appeared line is reached and new line should appear again. Same behaviour when scrolling back to lines that was visible before. Could anybody give me any optimalizing tip that can help to scroll smoothly.
In my adapter I am using optimalizing features as a holder class and getTag() in getView method. Images are not loading from web but from sd card by setImageURI method.
Anyway I am expecting the problem here. Loading a line - 6 background images in this way must take some time but I have no idea how to make a workaround. The avarage size of bg image is 50 KB. Here is my getView.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(mItemResId, parent, false);
holder = new ViewHolder();
holder.tvName = (TextView) convertView.findViewById(R.id.grid_item_name);
holder.tvPref = (TextView) convertView.findViewById(R.id.grid_item_pref);
holder.tvCustomText = (TextView) convertView.findViewById(R.id.grid_item_custom);
holder.ivBg = (ImageView) convertView.findViewById(R.id.grid_item_bg);
holder.ivSmallIcon = (ImageView) convertView.findViewById(R.id.grid_item_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Product item = getItem(position);
holder.tvName.setText(item.getName());
holder.tvPref.setText(item.getPreference());
holder.tvCustomText.setText(item.getCustomText());
if (item.isImportant()) {
holder.ivSmallIcon.setVisibility(View.VISIBLE);
} else holder.ivSmallIcon.setVisibility(View.GONE);
File f = new File( item.getImagePath() );
if ( f.exists() ) {
holder.ivBg.setImageURI( Uri.fromFile(f) );
} else {
holder.ivBg.setImageResource(R.drawable.no_img);
}
return convertView;
}
Or is there any chance I can prefetch not visible items or at least images before the getView method is called? Or anything else that can help?
Thank you very much.