【问题标题】:How to stop the progress bar after getting response from the server从服务器获得响应后如何停止进度条
【发布时间】:2019-08-24 14:46:32
【问题描述】:

在收到服务器响应后,我正在尝试停止进度条。 下面是我用过的xml代码

activity.xml

  <ProgressBar
        android:layout_weight="1"
        android:id="@+id/progressBar"
        android:indeterminate="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="40dp"
        android:minWidth="40dp"
        tools:ignore="MissingConstraints" />

activity.kt

 var progressBar =  findViewById(R.id.progressBar) as ProgressBar
 doAsync {
            try {
                    progressBar.setVisibility(View.VISIBLE);
                    appList = getAppList(requestBody, LAUNCHER_BASE_URL)
                    Log.d("App list =", appList.toString())
                    appInfoList = populateList()
                    progressBar.setVisibility(View.GONE);
            } catch (e: Exception) {
                progressBar.setVisibility(View.GONE);
                Log.e("Exception-launcher", e.toString())
            }

从上面的代码来看,即使收到响应,进度条也不会停止。任何帮助将不胜感激

我试过了,但是进度条停止后页面变成空白

【问题讨论】:

  • 因为你的进度条消失了,而它抛出了一些异常,而不是你获得成功响应的时候。
  • 所以你想在进度条停止后向用户显示响应?并且您的显示在进度条消失后变为空白。这意味着您没有将您的回复附加到您的设计中。

标签: android kotlin progress-bar


【解决方案1】:

在你的 try 子句中也添加progressBar.setVisibility(View.INVISIBLE);,因为在当前状态下,它只会在 API 失败时移除加载:

 var progressBar =  findViewById(R.id.progressBar) as ProgressBar
 doAsync {
            try {
                    progressBar.setVisibility(View.VISIBLE);
                    appList = getAppList(requestBody, LAUNCHER_BASE_URL)
                    Log.d("App list =", appList.toString())
                    appInfoList = populateList()
                progressBar.setVisibility(View.INVISIBLE);
            } catch (e: Exception) {
                progressBar.setVisibility(View.INVISIBLE);
                Log.e("Exception-launcher", e.toString())
            }

【讨论】:

  • 我试过了,进度条停止了,但问题是进度条消失后屏幕变成空白
  • 我不确定布局是如何布局的,但请尝试View.INVISIBLE 看看是否有任何改变。
  • 非常感谢,它正在工作。而不是View.GONE。我用过View.INVISIBLE
【解决方案2】:

试试下面的代码

doAsync {
    try {
        progressBar.visibility = View.VISIBLE
        appList = getAppList(requestBody, LAUNCHER_BASE_URL)
        Log.d("App list =", appList.toString())
        appInfoList = populateList()
    } catch (e: Exception) {
        progressBar.visibility = View.GONE
        Log.e("Exception-launcher", e.toString())
    } finally {
        progressBar.visibility = View.GONE
    }

【讨论】:

  • 我试过了,进度条停止了,但问题是进度条消失后屏幕变成空白
  • 我猜问题出在你的 xml 中
  • 您在上面附加的 xml 中也没有问题
  • 我猜是你的观点构成造成了问题,你应该尝试View.INVISIBLE而不是View.GONE
【解决方案3】:

仅当代码进入 Exception(捕获块)时,您才隐藏进度条。

将代码移到 catch 块之后,如下所示:

    try {
         progressBar.setVisibility(View.VISIBLE);
         appList = getAppList(requestBody, LAUNCHER_BASE_URL)
         Log.d("App list =", appList.toString())
         appInfoList = populateList()
    } catch (e: Exception) {
         Log.e("Exception-launcher", e.toString())
    }
    progressBar.setVisibility(View.GONE);

如果有帮助请告诉我

【讨论】:

  • 我试过了,进度条停止了,但问题是进度条消失后屏幕变成空白
  • @ChristinaVarghese 你能添加你的 XML 代码吗?
【解决方案4】:

不太清楚如何在doAsync 中处理View 通常this应该是这样表示的

 val progressBar =  findViewById<ProgressBar>(R.id.progressBar)
 progressBar.visibility = View.VISIBLE
 doAsync {
      try {
         appList = getAppList(requestBody, LAUNCHER_BASE_URL)
         Log.d("App list =", appList.toString())
         appInfoList = populateList()              
      } catch (e: Exception) {
          e.printStackTrace()
      }

      uiThread {
         progressBar.visibility = View.GONE
      }
 }

【讨论】:

    猜你喜欢
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 1970-01-01
    相关资源
    最近更新 更多