滚动网格视图时重复调用网格视图的第一个位置



我想知道为什么当我滚动网格视图时,即使我设置了适配器,也会反复调用第一个位置的视图。这导致我的网格视图无法流畅滚动,因为它反复重新加载图像。

似乎Picasso.with(context).load(file).into(viewImage);导致重复调用,因为如果我删除此行,网格视图可以平滑滚动。

有什么改进建议吗?谢谢。

网格视图阵列适配器的实现

class HomeBrandCVC extends ArrayAdapter<Brand> {
private Context context;
private ArrayList<Brand> brandArrayList;
private Map<Integer, File> fileMap = new HashMap<>();
public HomeBrandCVC(Context c, ArrayList<Brand> brandArrayList) {
super(c, R.layout.homecvc, brandArrayList);
this.context = c;
this.brandArrayList = brandArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.homecvc, parent, false);
TextView nameLabel = (TextView) rowView.findViewById(R.id.homecvc_nameLabel);
nameLabel.setVisibility(View.INVISIBLE);
final Brand brand = brandArrayList.get(position);
nameLabel.setText(brand.name);
ImageView viewImage = (ImageView) rowView.findViewById(R.id.homecvc_viewImage);
Log.d("hellllo", String.valueOf(position));
if (fileMap.containsKey(position)) {
viewImage.setImageBitmap(BitmapFactory.decodeFile(fileMap.get(position).getAbsolutePath()));
} else {
viewImage.setImageResource(R.drawable.loadingimage);
new MyLeanCloudApp.GetImageFromDiskOrUrl(brand.imageFile, new MyLeanCloudApp.ImageHandlerGetImagesResponse() {
@Override
public void processFinish(File file) {
if (context != null) {
if (file != null) {
Picasso.with(context).load(file).into(viewImage);
fileMap.put(position, file);
}
}
}
}).execute();
}
return rowView;
}
}

始终尝试对列表和 gird 使用ViewHolder模式。这将提高列表和网格的性能。其他人认为它不仅取决于毕加索,还取决于您的图像网址,您在xml布局文件中,加载数据等。

尝试使用ViewHolder以避免滚动问题并提高其性能

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView==null){
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.grid_item,parent,false);
convertView.setTag(viewHolder);
}
else {
viewHolder=(ViewHolder)convertView.getTag();
}
viewHolder.image=(ImageView)convertView.findViewById(R.id.thumbnail);
viewHolder.title=(TextView)convertView.findViewById(R.id.title);
viewHolder.title.setText(Html.fromHtml(values.getPosts().get(position).getTitle()));
imageView.setImageResource(Imageid[position]);
return convertView;
}
private static class ViewHolder{
public static ImageView image;
public static TextView title;
}

另一种想法是尝试从现金中加载您的图像,如果它之前在您使用毕加索时加载。为此,请查看本教程。

最新更新