2

I have class (MainActivity.java) with extends Activity, in the inside i declare Fragment class (FragmentShow.java) . I tried use GestureDetector in Fragment class, but this gesture not working, if I use GestureDetector in Activity class is working. The question is how to use GestureDetector in fragment class?

Thanks.

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

This I call Fragment from MainActivity

FragmentDashboard hello = new FragmentDashboard();
        FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
            .replace(R.id.fragment_container, hello)
                .commit(); 
  • 1
    Please show your code – Divers Jul 25 '16 at 08:57
  • Please refer http://stackoverflow.com/a/11421565/3527886 – Mayur Patel Jul 25 '16 at 09:09
  • public class MyGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { /* * Toast.makeText(getBaseContext(), event1.toString() + "\n\n" * +event2.toString(), Toast.LENGTH_SHORT).show(); * if (event2.getX() < event1.getX()) { Utils.log("Swipe left - startActivity()"); } else { Utils.log("Swipe Right - startActivity()");} return true;}} – Affa Musaffa Jul 26 '16 at 09:13
  • Show the code containing your fragment class – Ojonugwa Jude Ochalifu Jul 26 '16 at 09:24
  • Hi Mayur Patel , I can't resolve my problem with your refer – Affa Musaffa Jul 26 '16 at 09:26
  • ojonugwa ochalifu, I call fragment with this FragmentDashboard hello = new FragmentDashboard(); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.fragment_container, hello) .commit(); – Affa Musaffa Jul 26 '16 at 09:29
  • Are you getting any errors? – Ojonugwa Jude Ochalifu Jul 26 '16 at 09:35
  • If you want fast help, you need to post questions that are easy to understand.Right now, you are posting your classes together. Take a look at [this](http://stackoverflow.com/questions/32944798/switch-between-fragments-with-onnavigationitemselected-in-new-navigation-drawer/33096003#33096003) question I posted to get an idea of how to format your questions.Don't be in a rush – Ojonugwa Jude Ochalifu Jul 26 '16 at 09:41
  • @ojonugwaochalifu Sorry if my question is difficult to understand, but here I need answers , not questioning my question . Thx – Affa Musaffa Jul 28 '16 at 05:17
  • That's the attitude, keep it up. – Ojonugwa Jude Ochalifu Jul 29 '16 at 09:31
  • Check out [this question](https://stackoverflow.com/questions/11421368/android-fragment-oncreateview-with-gestures). It has the answer this question has asked. – N. Matic Sep 04 '18 at 22:05

0 Answers0