我正在尝试为Android中的小部件添加双击手势。我尝试the usual way向活动添加手势。它不起作用。我看到here in another Stackoverflow thread需要使用RemoteViews和Listview或这样的组件才能向小部件添加手势。但这个答案是在2011年。有更简单的方法吗?我没有在我的小部件中使用这些组件,但现在出现了这个新的手势要求。这需要我重写很多代码。 这是我试过的:
public class AppWidgetProvider_Basic
extends AppWidgetProvider
implements
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
...
private GestureDetectorCompat mDetector;
...
@Override
public void onEnabled(Context context) {
...
this.initializeGestureListener();
}
...
...
private void initializeGestureListener() {
Log.d(MEConstants.APP_NAME, "initializeGestureListener");
mDetector = new GestureDetectorCompat(this.currentContext, this);
mDetector.setOnDoubleTapListener(this);
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
Log.d(Constants.APP_NAME, "onDown");
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
Log.d(Constants.APP_NAME, "single tap");
// TODO Auto-generated method stub
return false;
}
...
}