【问题标题】:Gridview first position called repeatedly when scroll gridview滚动gridview时重复调用Gridview第一个位置
【发布时间】:2019-02-02 11:15:27
【问题描述】:

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

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

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

gridview数组适配器的实现

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;
}
}

【问题讨论】:

    标签: gridview android-arrayadapter


    【解决方案1】:

    始终尝试将ViewHolder 模式用于列表和网格。这将提高列表和网格的性能。其他人认为它不仅取决于Picasso,还取决于您的图片网址、您在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;
    }
    

    其他想法是尝试从现金加载您的图像,如果它以前在您使用毕加索时加载。为此检查此tutorial

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 2013-09-10
      相关资源
      最近更新 更多