LongPressPopup icon indicating copy to clipboard operation
LongPressPopup copied to clipboard

Change the dispatch click system

Open dvdciri opened this issue 4 years ago • 1 comments

Would you consider changing the way the library dispatches the click to the leaf nodes only by instead checking if non-leaf nodes have a click listener attached and giving priority to those? This means that if i have a ViewGroup with a click listener and some items inside, everywhere i lift my finger in this view group, the click will be dispatched to the view group instead of the children.

Alternatively we can also inject this behavior and make it custmizable.

Happy to open a PR for this and contribute just let me know what you think.

dvdciri avatar Nov 26 '20 15:11 dvdciri

My proposal would be to change this method like below:

private void dispatchClickToLeafsOnly(ViewGroup view, MotionEvent motionEvent) {

    // If no view or no children, return
    if (view == null || view.getChildCount() == 0) {
      return;
    }
    for (int i = 0; i < view.getChildCount(); i++) {
      View child = view.getChildAt(i);

      if (child instanceof ViewGroup && !child.hasOnClickListeners()) {

        // If a ViewGroup without click listener, search for leafs inside
        dispatchClickToLeafsOnly((ViewGroup) child, motionEvent);
      } else {

        // Else check if touch event is inside it
        if (LongPressPopupUtils.isTouchInsideView(child, motionEvent)) {

          if (mLongPressReleaseClickListener != null) {

            // Notify the given listener
            mLongPressReleaseClickListener.onClick(child);
          } else {

            // Else, dispatch the touch event on the view
            child.performClick();
          }
        } else if (child instanceof ViewGroup) {
          // If a ViewGroup without click listener, search for leafs inside
          dispatchClickToLeafsOnly((ViewGroup) child, motionEvent);
        }
      }
    }
  }

And a similar change for the focus one.

dvdciri avatar Nov 26 '20 16:11 dvdciri