0

我一直在努力解决这个错误,因为它不能完全重现。我有一个自定义回收器适配器,它从数据库加载值。它调用网络助手类来构建 URL 并使用 Ion 加载图像。这个错误似乎不受滚动速度的影响,但我认为它可能会受到一次对服务器的图像调用量的影响。

 public ThreadAdapter(Cursor cursor) {
    super(cursor);
}

@Override
public void onBindViewHolderCursor(RecyclerView.ViewHolder holder, Cursor cursor) {
    ThreadViewHolder threadViewHolder = (ThreadViewHolder)holder;

    networkHelper.getImage(threadViewHolder.image,
            cursor.getString(cursor.getColumnIndex(DbContract.ThreadEntry.COLUMN_THREAD_IMAGE_NAME),
            cursor.getString(cursor.getColumnIndex(DbContract.ThreadEntry.COLUMN_THREAD_IMAGE_EXTENSION))
    );
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.from(parent.getContext()).inflate(R.layout.card_thread, parent, false);
    ThreadViewHolder viewHolder = new ThreadViewHolder(view);
    return viewHolder;
}

class ThreadViewHolder extends RecyclerView.ViewHolder{
    public ImageView image;

    public ThreadViewHolder(View itemView) {
        super(itemView);
        image = (ImageView) itemView.findViewById(R.id.thread_image);
    }

}

https://gist.github.com/Shywim/127f207e7248fe48400b

public void getImage(ImageView imageView, String name, String extension){
    if (name != null && extension != null){
        String imageUrl = "example.com/" + name + extension;

        Log.d(LOG_TAG, "Image Url " + imageUrl);
        Ion.with(imageView)
                .fitCenter()
                .placeholder(R.drawable.ic_launcher)
                .error(R.drawable.error)
                .load(imageUrl);
    }else {
        imageView.setImageBitmap(null);
    }
}

它似乎正在正确构建 URL,并且仅在需要调用某些内容时才调用 Ion。我有时会看到占位符图像出现然后消失。我从来没有见过错误图像出现。我认为如果项目 A、项目 B 和项目 C 都有需要加载的图像并且都同时出现,那么加载它们失败的可能性就更大。

4

0 回答 0