Android: Child view sharing pressed state from its parent view in Jelly Bean

时光总嘲笑我的痴心妄想 提交于 2019-12-01 17:43:02

问题


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

来源:https://stackoverflow.com/questions/14179431/android-child-view-sharing-pressed-state-from-its-parent-view-in-jelly-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!