Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dispatchTouchEvent in Fragment in Android

I am trying to get swipe up and swipe down gestures working in Fragment. The same is working fine with activity. In Fragment, I have an issue with dispatchTouchEvent. How do I use dispatchTouchEvent in Fragment? Is there an equivalent way to achieve this?

@Override
    public boolean dispatchTouchEvent(MotionEvent me)
    {
         this.detector.onTouchEvent(me);
       return super.dispatchTouchEvent(me);
    }
like image 913
TheDevMan Avatar asked Apr 11 '15 01:04

TheDevMan


People also ask

How to Override dispatchTouchEvent in fragment in Android?

Fragments are attached to activity, not replacing activity. So you can still override dispatchTouchEvent in your fragment parent activity and pass any actions from there. Show activity on this post. If your goal is to detect/handle swipe, add touch event listener on the fragment's view after creating the view.

How do you handle touch events in a fragment?

fragment_test, container, false); view. setOnTouchListener(new View. OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if(event. getAction() == MotionEvent.

What is dispatchTouchEvent?

dispatchTouchEvent(MotionEvent) - This allows your Activity to intercept all touch events before they are dispatched to the window. • ViewGroup. onInterceptTouchEvent(MotionEvent) - This allows a ViewGroup to watch events as they are dispatched to child Views.

Can I override dispatchtouchevent in a fragment parent activity?

10 Fragments are attached to activity, not replacing activity. So you can still override dispatchTouchEvent in your fragment parent activity and pass any actions from there. For example:

What is a fragment in Android?

Fragments. A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment. The fragment’s view hierarchy becomes part of, or attaches to , ...

How do I Pass actions from a fragment to an activity?

Fragments are attached to activity, not replacing activity. So you can still override dispatchTouchEvent in your fragment parent activity and pass any actions from there. For example:

What is a fragment in a view hierarchy?

A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment. The fragment’s view hierarchy becomes part of, or attaches to, the host’s view hierarchy.


2 Answers

Fragments are attached to activity, not replacing activity. So you can still override dispatchTouchEvent in your fragment parent activity and pass any actions from there.

For example:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentByTag("MY_FRAGMENT_TAG");
    myFragment.doSomething();
    return super.dispatchTouchEvent(ev);
}
like image 64
Ineptus Avatar answered Sep 18 '22 11:09

Ineptus


If your goal is to detect/handle swipe, add touch event listener on the fragment's view after creating the view.

like image 36
Rax Avatar answered Sep 18 '22 11:09

Rax