java - 位于 Fragment 中的 WebView 中没有出现 ProgressBar,如何在 Android WebView 中显示 ProgressBar?

标签 java android webview progress-bar visibility

每当我输入 fragment 时,都会显示几秒钟的空白,然后 WebView 中的网页就会显示出来。进度条好像没有显示。

这是我的 WebView 和 ProgressBar 所在 fragment 的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".FragmentThree">

    <ProgressBar

        android:id="@+id/progressbar"
        style="@style/Widget.AppCompat.ProgressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"/>

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </WebView>

</RelativeLayout>

这里是 Fragment Java 实现的 Java 文件:

package com.example.task1;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;


public class FragmentThree extends Fragment {
    private WebView webView;
    private String URL;
    private ProgressBar progressBar;

    private OnFragmentInteractionListener mListener;

    public FragmentThree(String url) {
        // Required empty public constructor
        this.URL = url;
    }


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

    private void initMain(View view) {
        webView = view.findViewById(R.id.webview);
        progressBar = view.findViewById(R.id.progressbar);

        progressBar.setMax(100);
        webView.loadUrl(URL);
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                progressBar.setProgress(newProgress);
            }
        });

    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

为了便于说明,代码主要位于 initMain() 函数下。

非常感谢任何建议,感谢您的阅读!

最佳答案

只需在 Webview 下声明 ProgressBar 即可,如下所示

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".FragmentThree">



    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </WebView>
    <ProgressBar

        android:id="@+id/progressbar"
        style="@style/Widget.AppCompat.ProgressBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"/>
</RelativeLayout>

关于java - 位于 Fragment 中的 WebView 中没有出现 ProgressBar,如何在 Android WebView 中显示 ProgressBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58625061/

相关文章:

java - 缩小 WebView 初始比例

java - 为什么我的 Scanner 变量没有使用 : System. out.println(input.nextDouble()); 转换为 double 值?

Android:如何将观察者用于多个过滤条件?

java - Tumblr 无法在 WebView 中正确呈现

android - 如何通过jenkins运行appium测试脚本

android - 自定义 ProgressBar 旋转不流畅

android - 如何在 Android 中自动化 webview

Java为什么日历afieldset是假的?

java - 尝试连接到 FireStore 数据库但收到 "Program type already present: okio.Buffer$2"

java - Java中解析文本文件的问题