DispatchTouchEvent in Snippet on Android

I try to get gestures up and down while working in a fragment. The same thing works fine with activity. In the snippet, I have a problem with dispatchTouchEvent. How to 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); } 
+8
source share
3 answers

If your goal is to detect / process napkins, add an event listener for the event as a fragment after creating the view.

+4
source

Fragments are tied to activity, not to action. Thus, you can still override dispatchTouchEvent in the parent activity of the fragment and pass any actions from there.

For instance:

 @Override public boolean dispatchTouchEvent(MotionEvent ev) { MyFragment myFragment = (MyFragment) getFragmentManager().findFragmentByTag("MY_FRAGMENT_TAG"); myFragment.doSomething(); return super.dispatchTouchEvent(ev); } 
+4
source

You should send dispatchTouchEvent in your parent activity as follows. Add this code to parent activity:

 private List<MyOnTouchListener> onTouchListeners; @Override protected void onCreate(Bundle savedInstanceState) { if(onTouchListeners==null) { onTouchListeners=new ArrayList<>(); } } public void registerMyOnTouchListener(MyOnTouchListener listener){ onTouchListeners.add(listener); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { for(MyOnTouchListener listener:onTouchListeners) listener.onTouch(ev); return super.dispatchTouchEvent(ev); } public interface MyOnTouchListener { public void onTouch(MotionEvent ev); } 

OnSwipeTouchListener:

 public class OnSwipeTouchListener{ private final GestureDetector gestureDetector; public OnSwipeTouchListener (Context ctx){ gestureDetector = new GestureDetector(ctx, new GestureListener()); } private final class GestureListener extends SimpleOnGestureListener { //override touch methode like ondown ... //and call the impelinfragment() } public void impelinfragment(){ //this method impelment in fragment } //by calling this mehod pass touch to detector public void onTouch( MotionEvent event) { gestureDetector.onTouchEvent(event); } 

And add this code to the snippet you would like to add to it:

 //ontouch listenr MainActivity.MyOnTouchListener onTouchListener; private OnSwipeTouchListener touchListener=new OnSwipeTouchListener(getActivity()) { public void impelinfragment(){ //do what you want:D } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setting for on touch listener ((MainActivity)getActivity()).registerMyOnTouchListener(new MainActivity.MyOnTouchListener() { @Override public void onTouch(MotionEvent ev) { LocalUtil.showToast("i got it "); touchListener.onTouch(ev); } }); } 

I use this method to get all the movements to the right or left in the fragment, without conflict with other elements on the page .unlike rax answer

+4
source

Source: https://habr.com/ru/post/984973/


All Articles