You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用FirebaseImageLoader时Glide的.using方法无法解析问题求助

问题分析与解决

你遇到的.using()方法无法解析的问题,核心原因是Glide 4.x版本之后移除了.using()这个API,同时FirebaseUI的Glide集成方式也做了更新,不需要手动指定FirebaseImageLoader了。另外你的ViewHolder里的mContext也没有初始化,这也可能导致Glide加载时的上下文问题。下面一步步解决:

1. 核心原因:Glide API变更

在Glide 3.x中我们用.using()来指定自定义加载器,但Glide 4.x开始废弃了这个方法,改用自动注册ModelLoader的方式。FirebaseUI的最新版本已经适配了这个变化,只要你的依赖版本正确,直接加载StorageReference即可,FirebaseUI会自动使用FirebaseImageLoader处理。

2. 具体修复步骤

步骤一:检查依赖版本

确保你的firebase-ui-storage和Glide版本兼容,比如最新的依赖(以Android为例):

// FirebaseUI Storage
implementation 'com.firebaseui:firebase-ui-storage:8.0.2'
// Glide会被FirebaseUI自动引入,不用单独加,避免版本冲突

步骤二:修复ViewHolder的Context初始化

你的ViewHolder里声明了mContext但没有赋值,在ViewHolder构造函数里补上:

public ViewHolder(View itemView) {
    super(itemView);
    ButterKnife.bind(this, itemView);
    // 初始化mContext
    mContext = itemView.getContext();
}

步骤三:修改Glide加载代码

移除.using(new FirebaseImageLoader())这一行,直接加载StorageReference:

Glide.with(mContext)
     .load(match.getHomeTeam()) // 这里直接传入StorageReference即可
     .into(homeTeam);

// 客场队的加载也可以统一用mContext,不用每次拿awayTeam的Context
Glide.with(mContext)
     .load(match.getAwayTeam())
     .into(awayTeam);

3. 为什么这样可行?

FirebaseUI的firebase-ui-storage库已经包含了一个FirebaseGlideModule,它会自动向Glide注册FirebaseImageLoader,所以当你加载StorageReference类型的对象时,Glide会自动找到对应的加载器,不需要手动指定。

修改后的完整ViewHolder代码片段

static class ViewHolder extends RecyclerView.ViewHolder {
    private Context mContext;
    @BindView(R.id.imageView) ImageView homeTeam;
    @BindView(R.id.imageView2) ImageView awayTeam;
    @BindView(R.id.textView) TextView score;
    @BindView(R.id.textView2) TextView competition;
    @BindView(R.id.textView3) TextView game;

    public ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        // 初始化Context
        mContext = itemView.getContext();
    }

    public void bind(final DocumentSnapshot snapshot, final OnMatchSelectedListener listener) {
        Match match = snapshot.toObject(Match.class);
        Resources resources = itemView.getResources();

        // 修复后的Glide加载代码
        Glide.with(mContext)
             .load(match.getHomeTeam())
             .into(homeTeam);

        Glide.with(mContext)
             .load(match.getAwayTeam())
             .into(awayTeam);

        score.setText(match.getScore());
        competition.setText(match.getCompetition());
        game.setText(match.getGame());

        // Click listener
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onMatchSelected(snapshot);
                }
            }
        });
    }
}

内容的提问来源于stack exchange,提问作者flutter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:21:59