caseMotionEvent.ACTION_DOWN:mHasPerformedLongPress=false;if(performButtonActionOnTouchDown(event)){break;}// Walk up the hierarchy to determine if we're inside a scrolling container.booleanisInScrollingContainer=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=newCheckForTap();}postDelayed(mPendingCheckForTap,ViewConfiguration.getTapTimeout());}else{// Not inside a scrolling container, so show the feedback right awaymPrivateFlags|=PRESSED;//comment by branrefreshDrawableState();checkForLongClick(0);}break;
sdk16及以后:
123456789101112131415161718192021222324
caseMotionEvent.ACTION_DOWN:mHasPerformedLongPress=false;if(performButtonActionOnTouchDown(event)){break;}// Walk up the hierarchy to determine if we're inside a scrolling container.booleanisInScrollingContainer=isInScrollingContainer();// For views inside a scrolling container, delay the pressed feedback for// a short period in case this is a scroll.if(isInScrollingContainer){mPrivateFlags|=PFLAG_PREPRESSED;if(mPendingCheckForTap==null){mPendingCheckForTap=newCheckForTap();}postDelayed(mPendingCheckForTap,ViewConfiguration.getTapTimeout());}else{// Not inside a scrolling container, so show the feedback right awaysetPressed(true);checkForLongClick(0);}break;
后者会调用setPressed方法,代码如下:
1234567891011121314151617181920212223242526272829
publicvoidsetPressed(booleanpressed){finalbooleanneedsRefresh=pressed!=((mPrivateFlags&PFLAG_PRESSED)==PFLAG_PRESSED);if(pressed){mPrivateFlags|=PFLAG_PRESSED;}else{mPrivateFlags&=~PFLAG_PRESSED;}if(needsRefresh){refreshDrawableState();}dispatchSetPressed(pressed);}//viewgroup的实现protectedvoiddispatchSetPressed(booleanpressed){finalView[]children=mChildren;finalintcount=mChildrenCount;for(inti=0;i<count;i++){finalViewchild=children[i];// Children that are clickable on their own should not// show a pressed state when their parent view does.// Clearing a pressed state always propagates.if(!pressed||(!child.isClickable()&&!child.isLongClickable())){child.setPressed(pressed);}}}