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

Posted

技术标签:

【中文标题】ProgressBar 没有出现在位于 Fragment 的 WebView 中,如何在 WebView android 中显示 ProgressBar?【英文标题】:ProgressBar does not appear in WebView located in Fragment ,how to show ProgressBar in WebView android? 【发布时间】:2020-02-25 17:36:59 【问题描述】:

每当我输入片段时,都会显示几秒钟的空白区域,然后显示 WebView 中的网页。 ProgressBar 好像没有显示。

这是我的 WebView 和 ProgressBar 所在片段的 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_
    android:layout_
    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_
        android:layout_
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"/>

    <WebView
        android:id="@+id/webview"
        android:layout_
        android:layout_>

    </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() 函数下,以便澄清。

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

【问题讨论】:

在xml中尝试将进度条放在webview下方,否则webview会在进度条上方并覆盖它。 @JoachimHaglund 哦,我的上帝。编辑:也感谢您指出这一点。 【参考方案1】:

像这样在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_
    android:layout_
    android:orientation="vertical"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".FragmentThree">



    <WebView
        android:id="@+id/webview"
        android:layout_
        android:layout_>

    </WebView>
    <ProgressBar

        android:id="@+id/progressbar"
        style="@style/Widget.AppCompat.ProgressBar"
        android:layout_
        android:layout_
        android:layout_centerVertical="true"/>
</RelativeLayout>

【讨论】:

是的,刚刚从上面的评论中这样做了。不过谢谢你的回答!粗心大意是我的错。

以上是关于ProgressBar 没有出现在位于 Fragment 的 WebView 中,如何在 WebView android 中显示 ProgressBar?的主要内容,如果未能解决你的问题,请参考以下文章

在 ListView 中使用时,ProgressBar 性能不稳定

使用 CountDownTimer 更新 ProgressBar

ProgressBar 在 VBScript HTA 中没有得到刷新

如何使 ProgressBar 在 LibGDX 中工作?

在没有调试器的情况下运行时,ProgressBar 会“卡住”

WPF中ProgressBar 每0.1秒value+1如何实现?