为什么 setVisibility 在 Fragment/FrameLayout 上不起作用

Víc*_*tín 1 android visibility android-fragments android-framelayout

我有一个带有不确定进度条的布局,当超过 3 秒时,我想隐藏这个进度条并显示一个 web 视图,但由于某种原因它不起作用。

这是我的 XML:

<FrameLayout 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"
    android:id="@+id/fl_register_oauth"
    tools:context=".fragments.RegisterFragment"
    android:background="@color/f_yellow">

    <LinearLayout 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=".MainActivity$PlaceholderFragment"
        android:gravity="center"
        android:layout_gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/ll_pbar"
            android:gravity="center"
            android:visibility="gone">

        </LinearLayout>

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminateDrawable="@drawable/progress" />

        <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/webView"
            android:visibility="gone"
            android:layout_gravity="center" />

    </LinearLayout>

    <FrameLayout
        android:id="@+id/FlashBarLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginBottom="72dp">
        <!-- flash bar content -->
    </FrameLayout>

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

这是我的片段源代码。

public class RegisterFragment extends Fragment {

...

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_register, container, false);

        bar = (ProgressBar) view.findViewById(R.id.progressBar);

        webView = (WebView) view.findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);

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

我曾尝试使用带有 postDelayed 的处理程序来完成此操作,但我意识到它不起作用,尽管我将它从处理程序这一行中写出来。

bar.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助。

wes*_*ito 5

尝试:

((new Handler()).postDelayed(new Runnable() {
    @Override
    public void run() {
       bar.setVisibility(View.GONE);
       webView.setVisibility(View.VISIBLE);
    }
}, 3000);
Run Code Online (Sandbox Code Playgroud)

您不需要使用 runOnUiThread。问题是:您的代码立即执行并且活动开始时没有显示进度条。