Android webview 相关 进度 来回控制

 

 

   private String path = "https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=baidu";

    private long currentProgress;

    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ProgressBar bar = findViewById(R.id.bar);

        WebView webview = findViewById(R.id.webview);

        webview.loadUrl(path);

        webview.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }

        });


        webview.setOnTouchListener(new View.OnTouchListener() {

            private CountDownTimer countDownTimer;

            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                int action = motionEvent.getAction();

                switch (action){
                            case  MotionEvent.ACTION_DOWN:
                                if (countDownTimer != null) {
                                    countDownTimer.cancel();
                                    countDownTimer = null;

                                    if(currentProgress>=70*1000){
                                        return false;
                                    }
                                }

                                countDownTimer = new CountDownTimer(6 * 1000 , 500) {
                                    @Override
                                    public void onTick(long l) {
                                        currentProgress += 500;

                                        bar.setProgress((int) currentProgress);
                                    }

                                    @Override
                                    public void onFinish() {

                                    }
                                }.start();

                                break;
                            default:
                                break;
                        }


                return false;
            }
        });


    }

 

 

 

 

 

 

 

 

你可能感兴趣的:(Android webview 相关 进度 来回控制)