简体   繁体   中英

Why does not the reload() WebView when you change the position of touch

Good evening! Why does not Reload() by changing the touch position. After all, the code seems correct. From the code, I think you will understand what I need. Preferred is the easiest way to work around this problem, I just do not understand Java.

package com.example.pack.myapplication;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;


public class MainActivity extends ActionBarActivity implements View.OnTouchListener {

    float mX;
    float mY;
    float Start;
    float Fin;
    View rl;
    WebView web;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        web = (WebView) findViewById(R.id.webView2);
        web.loadUrl("http://example.com/page.html");
        web.getSettings().setJavaScriptEnabled(true);

        if (Start < Fin) {
            new Timer().schedule(new TimerTask(){
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    web.reload();
                }}, 1000, 1000);
        }

        rl = (View) findViewById(R.id.rl);
        rl.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mX = event.getX();
        mY = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
             Start = mY;
                break;
            case MotionEvent.ACTION_UP:
                Fin = mY;
                break;
        }
        return true;
    }
}

In advance thank you all.

There are a few things going wrong here. When you don't assign default values to floats they default to 0.0, so in your case Start and Fin both start out at 0.0. In Android onCreate is called when your Activity is created, which means that it only gets called once when you launch you app. That means when your onCreate is called Start and Fin are both 0.0, so Start < Fin is false so you never create the timer.

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