首页 > 解决方案 > 为什么 ProgressBar 以编程方式不可见?

问题描述

我对 ProgressBar 小部件有疑问。

这是布局中的 ProgressBar

...
<RelativeLayout 
    android:layout_height="match_parent"
    android:layout_width="match_parent">
<ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:indeterminate="true"
    android:layout_centerInParent="true"
    android:visibility="gone"
     />
</RelativeLayout>
....

这是活动,我有两种方法。

....
private ProgressBar mProgressBar;
private Handler handler = new Handler();
private int i = 0;
...
protected void onCreate(Bundle savedInstanceState) {
...
    mProgressBar = findViewById(R.id.progressBar);
... 
}

private void showProgressBar() {
    mProgressBar.setVisibility(View.VISIBLE);
}

private void hideProgressBar() {
    mProgressBar.setVisibility(View.GONE);
}

主要活动有一个菜单,在菜单上单击我想运行一个长任务,所以我想显示一个进度条

public boolean onOptionsItemSelected(MenuItem item) {
    showProgressBar();

    // This lines try to emulate a long task
    i = mProgressBar.getProgress();
    new Thread(new Runnable() {
        public void run() {
            while (i < 100) {
                i += 1;
                // Update the progress bar and display the current value in text view
                handler.post(new Runnable() {
                    public void run() {
                        mProgressBar.setProgress(i);
                        //txtView.setText(i+"/"+pgsBar.getMax());
                    }
                });
                try {
                    // Sleep for 100 milliseconds to show the progress slowly.
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();

函数调用下面的行是一个测试,我试图延迟显示并让我查看进度,但没有显示任何内容。

如果我更改布局并更改: android:visibility="visible" 或删除该行

当活动开始时,我可以看到进度,并且当到达 onOptionsItemSelected() 末尾的带有 hideProgressBar() 函数的行时,小部件被隐藏或消失。

问题出在哪里 ?关于线程?

我在虚拟设备中运行这个应用程序。我找不到一些关于显示动画的设置,但是因为如果可见性设置为 VISIBLE,我可以看到进度。我想这不是问题。

此致

编译 SDK:28 Android Studio:3.6.3

更新:完整布局源

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/filial_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/lista_filial_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:elevation="4dp"
            app:layout_scrollFlags="scroll|enterAlways"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <RelativeLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent">
    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:layout_centerInParent="true"
        android:visibility="gone"
         />
    </RelativeLayout>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_below="@+id/filial_appbar"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:context=".segregantes.ui.listafilial.ListaFilialActivity" />


</RelativeLayout>

更新 2:新代码。

i = mProgressBar.getProgress();
runOnUiThread(new Runnable() {
    @Override
    public void run() {
        while (i < 100) {
            i += 1;
            mProgressBar.setProgress(i);
            try {
                // Sleep for 100 milliseconds to show the progress slowly.
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
});

标签: androidprogress-bar

解决方案


您需要在主线程上执行 UI 操作,因此请在设置进度时尝试执行此操作

runOnUiThread(new Runnable() {
        public void run() {
             mProgressBar.setProgress(i);
        }
    });

推荐阅读