Android:子视图从Jelly Bean中的父视图共享压缩状态

jin*_*zju 5 android view touch-event

代码注释:1.RelativeLayout Clickable attr设置为true,其中有一个子视图,其Clickable attr设置为false.2.没有任何duplicateParentState attr,换句话说,duplicateParentState为false.3.子视图是TextView,其textColor是颜色选择器,因此它可以检查按下的状态.

行为:在level16之前,当单击RelativeLayout时,按下的状态不会传输到他的chlid视图.但是在16级或更高级别,它可以.

原因:setPressed ------> dispatchSetPressed ------>将压缩状态传递给子视图-----> childView.setPressed

15级的View.java onTouchEvent,

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;
Run Code Online (Sandbox Code Playgroud)

级别16中的View.java onTouchEvent,

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;
Run Code Online (Sandbox Code Playgroud)

请注意Bran的代码行,它们是不同的.setPressed(真); 不仅是mPrivateFlags | = PRESSED; 和refreshDrawableState(); ,但是dispatchSetPressed.

如果Google中有任何Android SDK开发人员,您想告诉我为什么要将mPrivateFlags | = PRESSED更改为setPressed(true);.