如何在Fragment类中使用手势检测器

时间:2016-07-25 08:54:41

标签: android android-fragments android-fragmentactivity swipe-gesture gesturedetector

我有使用extends Activity的类(MainActivity.java),在里面我声明了Fragment类(FragmentShow.java)。我尝试在Fragment类中使用GestureDetector,但这个手势不起作用,如果我在Activity类中使用GestureDetector正在工作。 问题是如何在片段类中使用GestureDetector?

感谢。

public boolean onTouchEvent(MotionEvent event) {
    this.gestureDetectorCompat.onTouchEvent(event);
    return super.getActivity().onTouchEvent(event);
}

public boolean dispatchTouchEvent(MotionEvent ev) {
    // TODO Auto-generated method stub
    // return super.dispatchTouchEvent(ev);
    super.getActivity().dispatchTouchEvent(ev);
    return gestureDetectorCompat.onTouchEvent(ev);
}

public class MyGestureListener extends
        GestureDetector.SimpleOnGestureListener {
    // handle 'swipe left' action only

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2,
            float velocityX, float velocityY) {

        if (event2.getX() < event1.getX()) {
            Utils.log("Swipe left - startActivity()");

        } else {
            Utils.log("Swipe Right - startActivity()");
        }

        return true;
    }
}

我从MainActivity调用Fragment

FragmentDashboard hello = new FragmentDashboard();
        FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
            .replace(R.id.fragment_container, hello)
                .commit(); 

0 个答案:

没有答案