如何解决在android WebView中长按时,哪种方法更适合在屏幕上的特定位置显示菜单?
我想在 WebView 中长按时在屏幕上的特定位置显示菜单。所以我设法使用以下两种方法来做到这一点。但我对性能和所有其他方面几乎没有疑问。从下面的 2 种方法中,哪一种更好?
方法 1: 这种方法使用 PointF 来存储 X 和 Y 坐标。
// class member variable to save the X,Y coordinates
private final PointF lastTouchDownXY = new PointF();
View.OnTouchListener onTouchListener = (v,event) -> {
// save the X,Y coordinates
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
lastTouchDownXY.set(event.getX(),event.getY());
}
// let the touch event pass on to whoever needs it
return false;
};
View.OnLongClickListener longClickListener = v -> {
// retrieve the stored coordinates
// use the coordinates for whatever
// holderUtility.onLongPress is a function which shows menu
holderUtility.onLongPress((int)lastTouchDownXY.x,(int)lastTouchDownXY.y);
// let the touch event pass on to whoever needs it
return false;
};
webView.setOnTouchListener(holder.onTouchListener);
webView.setOnLongClickListener(holder.longClickListener);
方法 2: 此方法使用 GestureDetector。
final View.OnTouchListener onTouchListener;
final GestureDetector gestureDetector;
gestureDetector = new GestureDetector(context,new GestureListener(activity,context));
onTouchListener = (v,event) -> {
gestureDetector.onTouchEvent(event);
return false;
};
final class GestureListener extends GestureDetector.SimpleOnGestureListener
{
public GestureListener( AppCompatActivity activity,Context context) {
this.activity = activity;
this.context = context;
}
@Override
public boolean onDown(MotionEvent e) {return true;}
@Override
public void onLongPress(MotionEvent event) {
// holderUtility.onLongPress is a function which shows menu
holderUtility.onLongPress((int)(event.getX()),(int)(event.getY()));
}
}
就性能和所有其他因素而言,哪种方法更好?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。