How does the fragment get the click event of the activity

 

        Nowadays, it is very common to use the fragment layout in the activity, so if you want to get the activity click event in the fragment, how to get it?

        There is a function to be done today, that is, there is a bullet box in the fragment, which needs to be hidden when the entire activity is touched, but the view in the fragment can only get the event that the fragment is touched, and cannot directly get the event of the activity Touch event, if we want to get the touch event of the activity, we can get the touch event of the activity in the fragment through callback, and then do the corresponding operation.

The specific operation is as follows:

1. Define the interface

Define an interface first

public interface FragmentKeyEventListener {
    void onFragmentKeyEvent(MotionEvent event);
}

 2. Implement the interface in the Fragment that needs to obtain the activity touch event, and implement the interface method.

public class WallpaperFragment implements FragmentKeyEventListener{

............................

    @Override
    public void onFragmentKeyEvent(MotionEvent event) {
            
     //do something what you want to do

    }

............................
}

3. Provide the setFragmentKeyEventListener() method in the activity and call it to the fragment to set the listener mFragmentKeyEventListener. The listener holds the onFragmentKeyEvent() method. After the activity is touched, it will automatically call the dispatchTouchEvent() method. In this method, use the listener to call the onFragmentKeyEvent() method, and then call back to step 2 and implement it in the fragment onFragmentKeyEvent() method, and execute the specific operations of the onFragmentKeyEvent() method in the fragment, the code is as follows:

public class MainActivity{

private FragmentKeyEventListener mFragmentKeyEventListener;

//Provide a method for the fragment to set the listener
public void setFragmentKeyEventListener(WallpaperFragment wallpaperFragment) {         this.mFragmentKeyEventListener = wallpaperFragment;  }

//The touch event obtained in the activity is called back to the fragment through the listener (that is, every time the activity is touched,
  the touch event is passed to the fragment through the set listener)
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {       
            if ( mFragmentKeyEventListener != null) {                 mFragmentKeyEventListener.onFragmentKeyEvent(ev);             }         return super.dispatchTouchEvent(ev);     }



}

 

4. Get the activity instance in the fragment, and then use the activity instance to call the setFragmentKeyEventListener(this) method to set the listener in step 3.

@Override
public void onResume() {
   super.onResume();
   ((MainActivity) mActivity).setFragmentKeyEventListener(this);
}

        Use the above four steps to complete the need to obtain the activity click event in the fragment.

おすすめ

転載: blog.csdn.net/weixin_42433094/article/details/126177332