简体   繁体   English

双击侦听器不能处理碎片

[英]Double tap listener is not working in fragments

Double tap listener is not working in fragment. 双击侦听器不在片段中工作。 Firstly I wrote the and implemented on sample project that is working fine in sample project.In sample project there is only one activity.But when I am trying to integrate that sample code into my project it is not working fine.My real project has an activity that is loading a fragment on it.My requirement is when I double tap any where on that fragment(loaded on activity) i want some functionality against it.The problem is double tap is not working.Here is the code I am using to achieve it. 首先,我在示例项目中编写并实现了示例项目中正常工作。在示例项目中只有一个活动。但是当我尝试将该示例代码集成到我的项目中时它不能正常工作。我的真实项目有一个在我上面加载一个片段的活动。我的要求是当我双击该片段上的任何位置(加载活动)时我想要一些功能来对付它。问题是双击不起作用。这是我用的代码实现这一目标。

@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);

    }
}

I am implementing GestureDetector.OnGestureListener in my Activity like this 我在我的Activity中实现了GestureDetector.OnGestureListener

public class FragmentActivity extends BaseClass
        implements View.OnClickListener, GestureDetector.OnGestureListener {}

and I am overriding onTouchEvent() method like this 我像这样重写onTouchEvent()方法

@Override
public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

I am not able to figure this thing out what i am missing.Trying to solve this for a long time but no luck. 我无法弄清楚我想念的东西。想要解决这个问题很长一段时间但没有运气。 Any help please. 请帮忙。

Do you really need to use GestureDetector? 你真的需要使用GestureDetector吗?
Сan you fit this option? 你适合这个选择吗?

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM