Android - Webview page requires double clicking -

2019-08-31 06:34发布

My app is simply a WebView to display a specific website. Users are not able to simply use the Chrome app because of the inherent programming in Android which alters the functions of tapping.

For simplicity, let's say the website displays a calendar and on that calendar are boxes. The boxes can either be single clicked to be selected or double clicked to be opened to display the event details.

The website has coding that handles what to do if an event is single or double clicked:

<div class="boardpiece clickable" onclick="select(event, this);" ondblclick="open('5657849');"

I'm completely fine with using longpress to simulate the double click. Here's where my confusion begins. How to I tell my app that when the user performs a SingleTap on the item, it needs to activate the "onclick" the website is calling for? I also need to tell the app that when the user performs a longpress on that same item, activate the "ondblclick" event you see there in the website coding?

Here is my activity code:

import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.SslErrorHandler;
import android.net.http.SslError;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;

public class MainActivity extends Activity{
    WebView webview;    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = (WebView) findViewById(R.id.webview);   
        //Do we want/need to enable Java?
        webview.getSettings().setJavaScriptEnabled(true); 
        //Here we allow for zoom controls - pinch
        webview.getSettings().setBuiltInZoomControls(true);
        //Here we remove the zoom control buttons - requires API 11
        webview.getSettings().setDisplayZoomControls(false);
        //Here we clear the Cache and SSL Preferences       
        webview.clearCache(true);
        webview.clearSslPreferences();
        //Do we need to enable scroll bars to allow people to scroll left and right?        
        webview.setHorizontalScrollBarEnabled(true);
        webview.setVerticalScrollBarEnabled(true);
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl("https://website");

        final GestureDetector gd = new GestureDetector(new MyGestureDetector());
        View.OnTouchListener gl = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gd.onTouchEvent(event);
            }
        };
        webview.setOnTouchListener(gl);
    }

    class MyGestureDetector extends SimpleOnGestureListener {    
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            Log.i("", "DoubleTap");
            return true;
        }   

        public boolean onSingleTap(MotionEvent e) {
            Log.i("", "SingleTap");
            return true;
        }
    }    


// Ignore SSL certificate errors    
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }

//Would like to have a Menu Button to refresh the page - or really just bring you to the login page - for use when the session times out    
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(1, 1, 0, "Reload");
    //getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
    case R.id.reload:
        webview.loadUrl("website");
        return true;
    }
    return super.onOptionsItemSelected(item);

}

}

1条回答
Luminary・发光体
2楼-- · 2019-08-31 07:09

I think you're looking at this wrong. Instead of detecting the double tap in Android and trying to inject it into your Javascript you could be detecting the double tap in Javascript. Just keep the time of the last tap and if it's been recently enough the second tap is part of a double tap.

查看更多
登录 后发表回答