Android - ProgressBar setVisibility to GONE无法正常工作

j.g*_*ima 10 android android-fragments android-spinner progress-bar

我一直ProgressBar在我的应用程序中添加片段.我将其设置为两个主要片段(用作选项卡),如下所示:

ProgressBaractivity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

设置ProgressBar VISIBLEGONE:

spinner = (ProgressBar)getActivity().findViewById(R.id.progressBar1);
spinner.setVisibility(View.VISIBLE);

spinner.setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

这没有任何问题.我试图将ProgressBar另一个片段添加到另一个片段,其中包含WebView:

ProgressBarfragment_article.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="info.androidhive.slidingmenu.ArticleFragment$PlaceholderFragment" >

    <WebView android:id="@+id/webPage"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

设置可见性:

spinner = (ProgressBar)getActivity().findViewById(R.id.progressBar1);
spinner.setVisibility(View.VISIBLE);

spinner.setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

以与前一代码相同的方式设置可见性,但由于某种原因,这不是设置ProgressBarGONE.不确定是什么问题.

我尝试使用Android中的clearAnimation建议,setVisbility在RelativeLayout中不起作用但仍然没有.

spinner.clearAnimation();
spinner.setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

Ill*_*ent 6

检查此代码:

spinner = (ProgressBar)getActivity().findViewById(R.id.progressBar1);
Run Code Online (Sandbox Code Playgroud)

如果您使用片段,它应该是这样的:

spinner = (ProgressBar)viewIinflated.findViewById(R.id.progressBar1);//same case with dialogs
Run Code Online (Sandbox Code Playgroud)

如果您正在使用活动,那么:

spinner = (ProgressBar)findViewById(R.id.progressBar1);
Run Code Online (Sandbox Code Playgroud)


Kev*_*Ryu 5

我有同样的问题(progressBar.setVisibility() 不起作用)。

正如@Illegal Argument 所说,

// in Activity
ProgressBar mProgressBar = (ProgressBar)findViewById(R.id.progressBar1);
mProgressBar.setVisibility(View.GONE); 
Run Code Online (Sandbox Code Playgroud)

如果该代码在 uiThread(mainThread) 上运行,则应该可以正常工作。

我的问题是我试图在 uiThread 上运行代码。所以我通过更改代码解决了这个问题

mProgressBar.setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        mProgressBar.setVisibility(View.GONE);
    }
});
Run Code Online (Sandbox Code Playgroud)