Android: How can i show progress bar while loading data into WebView?

后端 未结 4 1944
失恋的感觉
失恋的感觉 2021-01-06 10:21

How can I show the progress bar while loading data into my webview? My code :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInsta         


        
4条回答
  •  醉酒成梦
    2021-01-06 11:10

    First make custom webview class and use it on xml like this,

     
    
        
    

    Make sure, the height should not be fixed, it should be "wrap_content"

    Now make custom webview class like this,

     public class CustomWebView extends WebView {
    
        private ProgressBar progressBar;
        private Context context;
        private LinearLayout loaderLayout;
    
        public CustomWebView(Context context) {
        super(context);
    
        this.context = context;
        initProgressBar();
    
        loadUrlWithData();
    
    }
    
    public CustomWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    
        this.context = context;
        initProgressBar();
    
    }
    
    public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    
        this.context = context;
    
        initProgressBar();
    
    }
    
    private void initProgressBar() {
        progressBar = new ProgressBar(context);
        progressBar.setIndeterminate(false);
    
    }
    
    private void setLoading(boolean isLoading) {
    
      if (isLoading) {
    
       progressBar.setVisibility(View.VISIBLE);
    
       loaderLayout = new LinearLayout(context);
       loaderLayout.setGravity(Gravity.CENTER);
       loaderLayout.setOrientation(LinearLayout.HORIZONTAL);
    
        LinearLayout.LayoutParams parentLayoutParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            loaderLayout.setLayoutParams(parentLayoutParams);
    
            LinearLayout.LayoutParams progressBarLayoutParams = new 
            LinearLayout.LayoutParams(75, 75);
            progressBar.setLayoutParams(progressBarLayoutParams);
    
            if (progressBar.getParent() != null) {
                ((ViewGroup) progressBar.getParent()).removeView(progressBar);
            }
            loaderLayout.addView(progressBar);
    
            // add this loaderLayout to your parent view in which your webview is added
    
            else {
    
               progressBar.setVisibility(View.GONE);
            }
    
      }
    
      private void loadUrlWithData(){
         setLoading(true);
         //Load your HTML data with url here
    
      }
    
     //When the content will be loaded, the webview will change it's height
     @Override
    protected void onSizeChanged(int w, int h, int ow, int oh) {
        super.onSizeChanged(w, h, ow, oh);
    
        if (h > 400) {
            setLoading(false);
        }
    }
    
    }
    

提交回复
热议问题