5

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

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

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

View.java onTouchEvent 在级别 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 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;

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

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

4

0 回答 0