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);
}
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.
fragment_test, container, false); view. setOnTouchListener(new View. OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if(event. getAction() == MotionEvent.
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.
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:
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 , ...
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:
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.
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);
}
If your goal is to detect/handle swipe, add touch event listener on the fragment's view after creating the view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With