【问题标题】:Smooth scroll to position in Android gridview makes work it wrong平滑滚动到 Android gridview 中的位置会使工作出错
【发布时间】:2014-09-15 17:52:35
【问题描述】:

我有一个应用程序将图像从 sdcard 加载到我的主屏幕上的 gridview。我创建了 ViewHolder,所以视图只实例化一次。在我使用smoothScrollToPosition 方法之前,一切都很好。

当活动恢复或创建时,我调用smoothScrollToPosition,因此用户网格立即滚动到所选位置。问题是它会加载位于网格开头的图像。

假设我有包含 20 个网格的 GridView。只有 2 个有图像,第一个和第二个,其他是空的(具有默认图像集)。现在在活动开始时,我想平滑滚动到 12 位置。它应该是空的,但 12 和 13 网格包含来自第一个和第二个网格的图像。如果此网格上已有要加载的图像,则不存在此问题。

我的适配器是典型的:

 public View getView(int position, View convertView, ViewGroup parent) {
    dayNumber = dayList.get(position).getDayId();
    imagePath = dayList.get(position).getPhotoPath();

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(R.layout.gridview_cell, parent, false);

        holder = new ViewHolder();
        holder.day = (TextView) convertView.findViewById(R.id.dayDateTextView);
        holder.image = (ImageView) convertView.findViewById(R.id.dayImage);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    setDayNumber(dayNumber);
    setCurrentDay();
    setBitmap(imagePath, parent, position);

    return convertView;
}

我将不胜感激。谢谢

编辑:

还有剩下的适配器:

private void setBitmap(String imagePath, ViewGroup parent, int position) {
    if (imagePath != null) {
        setBitmapFromSd(parent, position);
    } else {
        holder.image.setImageResource(R.drawable.ic_action_camera);
    }
}

    private void setBitmapFromSd(ViewGroup parent, int position) {
    Bitmap bitmap = getBitmapFromMemCache(imagePath);
    if (bitmap != null) {
        holder.image.setImageBitmap(bitmap);
    } else {
        BitmapWorkerTask task = new BitmapWorkerTask(holder.image, parent, this, dayList.get(position));
        task.execute(imagePath);
    }
}

这是 BitmapWorkerTask :

public BitmapWorkerTask(ImageView imageView, ViewGroup parent, PhotoAdapter photoAdapter, Day day) {
    imageViewReference = new WeakReference<ImageView>(imageView);
    this.photoAdapter= photoAdapter;
    this.parent = parent;
    this.day = day;
}

protected Bitmap doInBackground(String... path) {
    Bitmap bitmap = null;
    try {
        bitmap = PhotoUtils.decodeSampledBitmapFromFile(parent, path[0]);
        photoAdapter.addBitmapToMemoryCache(path[0], bitmap);
    } catch (Exception e) {
        dbAdapter.setDayImage(null, day.getDayId(), day.getMonthId(), day.getYearId());
    }
    return bitmap;
}

@Override
protected void onPostExecute(Bitmap bitmap) {
    if (imageViewReference != null && bitmap != null) {
        final ImageView imageView = imageViewReference.get();
        if (imageView != null) {
            imageView.setImageBitmap(bitmap);
        }
    }
}

【问题讨论】:

  • 您是否使用库来下载图像?
  • 不,简单的BitmapFactory.decodeFile(currentPhotoPath, options);。我使用的唯一库是stickygridheaders
  • 您可以发布其余的 getView 方法吗?特别是你设置图片资源的地方
  • @hoomi,我已经编辑了主要问题
  • 您的 BitmapWorkerTask 中有 onPostExecute 吗?我猜你正在更新那里的图像视图。

标签: android gridview


【解决方案1】:

我建议你这样做:

  1. getView 方法中你'holder.image.setTag(imagePath);`
  2. BitmapWorkerTask 中添加另一个字段并将其命名为imagePath
  3. doInBackground 中添加this.imagePath = path[0];
  4. onPostExecute 方法中添加以下行:

    if (imageView != null) {
        String imagePath = (String)imageView.getTag();
        if (imagePath.equals(this.imagePath) {
             imageView.setImageBitmap(bitmap);    
        }
    }
    

我希望这能解决您的问题

【讨论】:

  • 谢谢,我已经尝试过了,但不幸的是它仍然无法正常工作。目前唯一的解决方案是删除smoothScroll方法......看起来这个smoothScrollToPosition混淆了适配器中的位置ID结构。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
  • 2011-06-24
相关资源
最近更新 更多