2

我正在创建一个带有 WebView 和页面顶部菜单的应用程序。我使该菜单不可见,并且仅在单击按钮时才会出现。这工作得很好,但那个按钮不能很好地适应任何地方,所以我没有点击一个按钮,所以当你双击屏幕时菜单会出现。所以这就是问题所在。双击仅适用于空白空间,但不适用于 WebView。我将如何让它工作,所以每当我双击屏幕时它就会出现/消失?

public class MainActivity extends Activity implements OnGestureListener{

private WebView webView;
LinearLayout toolbar;
private GestureDetector gd;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);

    webView =((WebView)findViewById(R.id.webView1));
    webView.setWebViewClient(new WebViewClient());
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    webView.setVerticalScrollBarEnabled(true);

    webView.loadUrl("URL_HERE");

    gd = new GestureDetector(this);

    gd.setOnDoubleTapListener(new OnDoubleTapListener()  
    {  
        @Override  
        public boolean onDoubleTap(MotionEvent e)  
        {  
            LinearLayout toolbar= (LinearLayout)findViewById(R.id.toolbar);

            if(toolbar.getVisibility() == View.GONE){
                toolbar.setVisibility(View.VISIBLE);

            }
            else {
                toolbar.setVisibility(View.GONE);

            }
            return false;  
        }  

        @Override  
        public boolean onDoubleTapEvent(MotionEvent e)  
        {  
            return false;  
        }  

        @Override  
        public boolean onSingleTapConfirmed(MotionEvent e)  
        {  
            return false;  
        }  
    });  


}

@Override  
public boolean onTouchEvent(MotionEvent event)  
{  
    return gd.onTouchEvent(event);//return the double tap events  
}  

@Override  
public boolean onDown(MotionEvent e)  
{  
    return false;  
}  

@Override  
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)  
{  
    return false;  
}  

@Override  
public void onLongPress(MotionEvent e)  
{  
}  

@Override  
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)  
{  
    return false;  
}  

@Override  
public void onShowPress(MotionEvent e)  
{  
}  

@Override  
public boolean onSingleTapUp(MotionEvent e)  
{  
    return false;  
}  

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

任何帮助将不胜感激。

4

2 回答 2

3

你试过添加这个吗?

webView.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gd.onTouchEvent(event);
    }
});
于 2013-10-18T11:25:59.303 回答
0

一个稍微简单的配置:

public class DoubleTapDemonstration extends activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_layout);

        // Create a `GestureDetector` that does something special on double-tap.
        final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onDoubleTap(MotionEvent event) {
                // TODO: Insert code to run on double-tap here.

                // Consume the double-tap.
                return true;
            }
        });

        // Get a handle for the `WebView`.
        WebView webView = (WebView) findViewById(R.id.webview);

        // Set the `WebView` to pass touch events through `gestureDetector`.
        webView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        });
    }
}
于 2016-11-21T13:56:29.367 回答