双击侦听器无法处理片段。首先,我在示例项目中编写并实现了示例项目中正常工作。在示例项目中只有一个活动。但是当我尝试将该示例代码集成到我的项目中时它不能正常工作。我的真实项目有一个在我上面加载一个片段的活动。我的要求是当我双击该片段上的任何位置(加载活动)时我想要一些功能来对付它。问题是双击不起作用。这是我用的代码实现它。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If user is not logged in
if (new SessionHelper(this).getCurrentUser() <= 0)
startActivity(new Intent(this, LoginScreen.class));
else {
mFragmentActivity = this;
// Broadcast receiver initialized here
mWifiStateReceiver = new WifiStateReceiver(mFragmentActivity);
setContentView(R.layout.activity_fragment);
initialize();
setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayShowTitleEnabled(false);
setClickListerns();
gestureDetector = new GestureDetector(this, this);
gestureDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
LogHelper.debugLog("single tap");
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
LogHelper.debugLog("doubletap");
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
});
// Check if redirected via push notification
if (getIntent().getExtras() != null && getIntent().getExtras().getBoolean("Notification", false))
replaceFragemtn(new StatementOfAccount(), false);
else
replaceFragemtn(new HomeFragment(), false);
}
}
我正在我的活动中实施 GestureDetector.OnGestureListener
public class FragmentActivity extends BaseClass
implements View.OnClickListener, GestureDetector.OnGestureListener {}
我正在重写 onTouchEvent()这样的方法
@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
我无法弄明白我想念的东西。想要解决这个问题很长一段时间但没有运气。请帮忙。
答案 0 :(得分:0)
你真的需要使用GestureDetector吗? 你适合这个选择吗?
public static abstract class DoubleClickListener implements View.OnClickListener {
private static final long DOUBLE_CLICK_TIME_DELTA = 300;
private long lastClickTime = 0;
@Override
public void onClick(View v) {
long clickTime = System.currentTimeMillis();
if (clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA) {
onDoubleClick(v);
} else {
onSingleClick(v);
}
lastClickTime = clickTime;
}
public abstract void onSingleClick(View v);
public abstract void onDoubleClick(View v);
}