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 {
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
source share