我正在创建一个带有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;
}
}
任何帮助将不胜感激。
答案 0 :(得分:3)
您尝试添加此内容吗?
webView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gd.onTouchEvent(event);
}
});
答案 1 :(得分: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);
}
});
}
}