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

Android中Picasso回调场景下ProgressBar无法显示问题

问题分析与解决方案

我来帮你排查下ProgressBar不显示的问题,主要根源在于线程逻辑混乱和静态UI引用的不合理使用,下面是具体的分析和修复步骤:

问题根源拆解

  1. AsyncTask与Volley的冗余混合:你用AsyncTask包裹了Volley请求,但Volley本身就是异步框架,这种混合会导致线程生命周期冲突,UI更新的时机不可控。
  2. 静态UI元素引用风险:直接使用ProductActivity.prod_img、ProductActivity.progressBar这类静态引用,不仅容易引发内存泄漏,还可能因为线程同步问题导致UI状态更新失效。
  3. ProgressBar显示逻辑缺少明确触发:虽然XML里设置了ProgressBar为visible,但如果Picasso从缓存快速加载图片,onSuccess会立刻隐藏它;如果是网络加载也不显示,大概率是UI元素的引用或线程上下文问题。

具体修复步骤

步骤1:移除冗余的AsyncTask,直接在Activity中发起Volley请求

把请求逻辑移到ProductActivity内部,避免AsyncTask带来的线程混乱:

// 在ProductActivity的onCreate中初始化UI并发起请求
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_product_layout);
    
    // 初始化UI元素(替换你的静态引用)
    ImageView prod_img = findViewById(R.id.prod_img);
    ProgressBar progressBar = findViewById(R.id.progressBar);
    TextView prod_name = findViewById(R.id.product_name);
    
    // 发起产品数据请求
    fetchProductData("你的产品接口URL");
}

private void fetchProductData(String url) {
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
            response -> {
                try {
                    parseProductResponse(response);
                } catch (JSONException | ParseException e) {
                    e.printStackTrace();
                }
            },
            error -> {
                // 请求失败时隐藏ProgressBar
                progressBar.setVisibility(View.GONE);
                // 可添加错误提示逻辑
            });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jsonObjectRequest);
}

步骤2:修正Picasso的加载逻辑,确保ProgressBar正确显隐

在加载图片前明确显示ProgressBar,回调中再更新状态:

private void parseProductResponse(JSONObject response) throws JSONException, ParseException {
    // 这里保留你的JSON解析逻辑,获取image_info[0]、name等数据
    
    // 计算图片尺寸(替换原有的屏幕宽高逻辑)
    int width, height;
    if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
        width = getScreenWidth(this);
        height = getScreenHeight(this)/2 - 50;
    } else {
        width = getScreenWidth(this);
        height = getScreenHeight(this) - 200;
    }

    // 更新产品名称等UI
    prod_name.setText(name);

    // 显式触发ProgressBar显示
    progressBar.setVisibility(View.VISIBLE);
    prod_img.setVisibility(View.GONE);

    // 用Activity上下文初始化Picasso,确保回调在主线程执行
    Picasso.with(this)
            .load(image_info[0])
            .error(R.mipmap.ic_launcher)
            .resize(width, height)
            .centerInside()
            .onlyScaleDown()
            .into(prod_img, new Callback() {
                @Override
                public void onSuccess() {
                    prod_img.setVisibility(View.VISIBLE);
                    progressBar.setVisibility(View.GONE);
                }

                @Override
                public void onError() {
                    progressBar.setVisibility(View.GONE);
                    // 加载失败时的提示逻辑
                }
            });
    
    // 保留其他UI更新逻辑(价格、描述等)
}

// 屏幕宽高工具方法
private int getScreenWidth(Context context) {
    return ((WindowManager) context.getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getWidth();
}

private int getScreenHeight(Context context) {
    return ((WindowManager) context.getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getHeight();
}

步骤3:优化布局,确保ProgressBar层级正确

在RelativeLayout中,把ProgressBar写在ImageView之后(XML中后定义的View层级更高),避免被隐藏的ImageView意外遮挡:

<RelativeLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/logo_img">
    <TextView android:id="@+id/product_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textSize="24sp"
        android:layout_marginTop="20dp"
        android:textColor="@color/black"
        android:text="Product name"
        android:gravity="center" />
    <!-- 先定义ImageView -->
    <ImageView android:id="@+id/prod_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:layout_marginTop="10dp"
        android:scaleType="fitXY"
        android:visibility="gone"
        android:layout_below="@id/product_name"/>
    <!-- 后定义ProgressBar,确保层级更高 -->
    <ProgressBar android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_centerHorizontal="true"
        android:visibility="visible"
        android:backgroundTint="@color/brumanoGolden"
        android:layout_below="@id/product_name"/>
</RelativeLayout>

步骤4:升级依赖(可选但推荐)

你的Picasso版本较旧,建议升级到最新稳定版,减少潜在问题:

implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
implementation 'com.android.volley:volley:1.2.1'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 06:57:53