Android: Child view sharing pressed state from its

2019-02-22 08:00发布

code comment: 1. A RelativeLayout Clickable attr is set to true, which has a child view whose Clickable attr is set to false. 2. No any duplicateParentState attr, in other words, duplicateParentState is false. 3. The child view is TextView whose textColor is color selector, so it can check pressed state.

behaviour: before level16, when clicking RelativeLayout, pressed state is not transmitted to his chlid view. However in level 16 or later, it can.

Reason: setPressed------>dispatchSetPressed------>transmit pressed state to children view----->childView.setPressed

View.java onTouchEvent in level 15,

case MotionEvent.ACTION_DOWN:
                mHasPerformedLongPress = false;

                if (performButtonActionOnTouchDown(event)) {
                    break;
                }

                // Walk up the hierarchy to determine if we're inside a scrolling container.
                boolean isInScrollingContainer = isInScrollingContainer();

                // For views inside a scrolling container, delay the pressed feedback for
                // a short period in case this is a scroll.
                if (isInScrollingContainer) {
                    mPrivateFlags |= PREPRESSED;
                    if (mPendingCheckForTap == null) {
                        mPendingCheckForTap = new CheckForTap();
                    }
                    postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
                } else {
                    // Not inside a scrolling container, so show the feedback right away
                    mPrivateFlags |= PRESSED; //comment by bran
                    refreshDrawableState();
                    checkForLongClick(0);
                }
                break;

View.java onTouchEvent in level 16,

case MotionEvent.ACTION_DOWN:
                 mHasPerformedLongPress = false;

                 if (performButtonActionOnTouchDown(event)) {
                     break;
                 }

                 // Walk up the hierarchy to determine if we're inside a scrolling container.
                 boolean isInScrollingContainer = isInScrollingContainer();

                 // For views inside a scrolling container, delay the pressed feedback for
                 // a short period in case this is a scroll.
                 if (isInScrollingContainer) {
                     mPrivateFlags |= PREPRESSED;
                     if (mPendingCheckForTap == null) {
                         mPendingCheckForTap = new CheckForTap();
                     }
                     postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
                 } else {
                     // Not inside a scrolling container, so show the feedback right away
                     setPressed(true); //comment by bran
                     checkForLongClick(0);
                 }
                 break;

Please notice the code line by Bran, they are different. setPressed(true); is not only mPrivateFlags |= PRESSED; and refreshDrawableState(); , but dispatchSetPressed.

If any Android SDK developer in Google, would you like to tell me why you change mPrivateFlags |= PRESSED to setPressed(true);.

0条回答
登录 后发表回答