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

API请求期间显示加载布局并动态修改TextView内容的实现问题

解决方案

1. 调整布局结构(实现整体显示/隐藏控制)

将Lottie动画和TextView封装到一个独立容器中,这样可以一键控制整个加载视图的显示与隐藏,修改后的布局代码如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#091A24">

    <!-- 加载视图容器,默认隐藏 -->
    <RelativeLayout
        android:id="@+id/loading_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        android:background="#091A24">

        <com.airbnb.lottie.LottieAnimationView
            android:id="@+id/lottiLoadingProgress"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_above="@+id/loading_splashscreenText"
            android:layout_marginBottom="15dp"
            app:lottie_autoPlay="true"
            app:lottie_loop="true" />

        <TextView
            android:id="@+id/loading_splashscreenText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_marginLeft="35dp"
            android:layout_marginRight="35dp"
            android:background="@color/black"
            android:fontFamily="@font/outfitsemibold"
            android:padding="5dp"
            android:text="Loading ... "
            android:textColor="@color/white"
            android:textSize="16dp" />
    </RelativeLayout>

    <!-- 此处放置请求成功后显示的主内容布局 -->
    <!-- ... -->
</RelativeLayout>

2. Kotlin代码实现逻辑

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.airbnb.lottie.LottieAnimationView
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivity : AppCompatActivity() {
    private lateinit var loadingContainer: View
    private lateinit var loadingText: TextView
    private lateinit var lottieView: LottieAnimationView

    // 随机加载文本集合
    private val loadingTexts = arrayOf(
        "正在加载数据...",
        "稍等片刻...",
        "努力获取资源中...",
        "数据加载中..."
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 通过ID获取控件
        loadingContainer = findViewById(R.id.loading_container)
        loadingText = findViewById(R.id.loading_splashscreenText)
        lottieView = findViewById(R.id.lottiLoadingProgress)

        // 发起API请求
        fetchDataFromApi()
    }

    private fun fetchDataFromApi() {
        // 显示加载布局
        loadingContainer.visibility = View.VISIBLE
        // 设置随机文本
        loadingText.text = loadingTexts.random()

        // 协程发起网络请求(可替换为Retrofit/OkHttp等框架)
        CoroutineScope(Dispatchers.IO).launch {
            try {
                // 模拟API请求延迟
                kotlinx.coroutines.delay(2000)
                // 实际API请求逻辑:val response = apiService.fetchData()

                // 请求成功,切换主线程更新UI
                withContext(Dispatchers.Main) {
                    loadingContainer.visibility = View.GONE
                    // 显示主内容:mainContent.visibility = View.VISIBLE
                }
            } catch (e: Exception) {
                // 请求失败处理
                withContext(Dispatchers.Main) {
                    loadingContainer.visibility = View.GONE
                    // 可添加错误提示逻辑
                }
            }
        }
    }
}

3. Java代码实现逻辑

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.airbnb.lottie.LottieAnimationView;
import java.util.Random;
import kotlinx.coroutines.Dispatchers;
import kotlinx.coroutines.GlobalScope;
import kotlinx.coroutines.launch;
import kotlinx.coroutines.withContext;

public class MainActivity extends AppCompatActivity {
    private View loadingContainer;
    private TextView loadingText;
    private LottieAnimationView lottieView;
    private String[] loadingTexts = {
            "正在加载数据...",
            "稍等片刻...",
            "努力获取资源中...",
            "数据加载中..."
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 通过ID获取控件
        loadingContainer = findViewById(R.id.loading_container);
        loadingText = findViewById(R.id.loading_splashscreenText);
        lottieView = findViewById(R.id.lottiLoadingProgress);

        // 发起API请求
        fetchDataFromApi();
    }

    private void fetchDataFromApi() {
        // 显示加载布局
        loadingContainer.setVisibility(View.VISIBLE);
        // 设置随机文本
        Random random = new Random();
        loadingText.setText(loadingTexts[random.nextInt(loadingTexts.length)]);

        // 协程发起网络请求(可替换为Retrofit/OkHttp等框架)
        GlobalScope.launch(Dispatchers.IO) {
            try {
                // 模拟API请求延迟
                Thread.sleep(2000);
                // 实际API请求逻辑:ApiResponse response = apiService.fetchData();

                // 请求成功,切换主线程更新UI
                withContext(Dispatchers.Main, () -> {
                    loadingContainer.setVisibility(View.GONE);
                    // 显示主内容:mainContent.setVisibility(View.VISIBLE);
                });
            } catch (Exception e) {
                // 请求失败处理
                withContext(Dispatchers.Main, () -> {
                    loadingContainer.setVisibility(View.GONE);
                    // 可添加错误提示逻辑
                });
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Awos Al-Radaideh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 08:04:55