Ho creato una piccola attività che è in grado di caricare due diverse stringhe HTML in una webview. Quando eseguo l'attività, inizia caricando la stringa dalla variabile page_1. Fin qui tutto bene. La pagina mostra come previsto. Ho aggiunto un listener onFling che dovrebbe consentire all'attività di caricare il contenuto della variabile page_2. Il problema è che anche se viene chiamato onFling e si chiama loadUrl, la webview non viene aggiornata?WebView non carica il mio codice HTML la seconda volta?
La mia attività è simile al seguente:
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.webkit.WebView;
public class Test extends Activity {
private GestureDetector mGestureDetector;
private WebView mWebView;
private int mPageIndex;
private static final String page_1 = "<html><body>Hello page 1</body></html>";
private static final String page_2 = "<html><body>Hello page 2</body></html>";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.loadData(page_1, "text/html", "utf-8");
setupGestureDetection();
mPageIndex = 0;
}
private void setupGestureDetection() {
mGestureDetector = new GestureDetector(new MyGestureDetector());
mWebView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
});
}
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_DISTANCE_THRESHOLD = 120;
private static final int SWIPE_VELOCITY_THRESHOLD = 200;
private boolean isHorizontalSwipe(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (Math.abs(e1.getX() - e2.getX()) > SWIPE_DISTANCE_THRESHOLD) {
return true;
}
}
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (isHorizontalSwipe(e1, e2, velocityX, velocityY)) {
if (e1.getX() > e2.getX()) {
// Right to left
if (++mPageIndex % 2 == 0) {
mWebView.loadData(page_1, "text/html", "utf-8");
} else {
mWebView.loadData(page_2, "text/html", "utf-8");
}
return true;
}
}
return false;
}
}
}
mio layout si presenta così:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Spero che qualcuno mi può aiutare! :-)
migliori saluti
Stig Andersen
È possibile ottenere output di errore dalla console Web se si aggiunge un [WebChromeClient] (http: // sviluppatore .android.com/riferimento/android/webkit/WebChromeClient.html). 'mWebView.setWebChromeClient (new WebChromeClient());' dovrebbe farlo. – dokkaebi
Inoltre, come si conoscono le chiamate loadData effettuate? Stai facendo un passo con il debugger? Stampa dei messaggi di registro? – dokkaebi