简体   繁体   中英

Floating Refresh-Button for WebView

I want to make a floating Refresh-Button for my WebView. I did some research but I wasn't able to fix my problem.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    String url = "https://www.rtl.de/cms/sendungen/serie/alarm-fuer-cobra-11.html";
    WebView view = (WebView) this.findViewById(R.id.WebView);
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl(url);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view.reload();
        }
    });

I also tried MainActivity.this.webView.loadUrl("https://www.rtl.de/cms/sendungen/serie/alarm-fuer-cobra-11.html"); instead of view.reload(); but there is always an error.

Cannot resolve method 'reload()'

or

Cannot resolve symbol 'WebView'

It would be very nice if anyone could help me.

You can actually just make an Intent and start your current activity as a new one. For example in your activity, make a button like this:

myButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, MainActivity.class);
            startActivity(intent);
        }
    });

Please tell me if it is ok.
Take care!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM