dispatchTouchEvent in Fragment in Android

后端 未结 3 1049
深忆病人
深忆病人 2021-01-17 11:49

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 d

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-17 12:17

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

提交回复
热议问题