publicObjectinstantiateItem(ViewGroupcontainer,intposition){if(mCurTransaction==null){mCurTransaction=mFragmentManager.beginTransaction();}finallongitemId=getItemId(position);// Do we already have this fragment?Stringname=makeFragmentName(container.getId(),itemId);Fragmentfragment=mFragmentManager.findFragmentByTag(name);if(fragment!=null){if(DEBUG)Log.v(TAG,"Attaching item #"+itemId+": f="+fragment);mCurTransaction.attach(fragment);}else{fragment=getItem(position);if(DEBUG)Log.v(TAG,"Adding item #"+itemId+": f="+fragment);mCurTransaction.add(container.getId(),fragment,makeFragmentName(container.getId(),itemId));}if(fragment!=mCurrentPrimaryItem){fragment.setMenuVisibility(false);fragment.setUserVisibleHint(false);}returnfragment;}
publicObjectinstantiateItem(ViewGroupcontainer,intposition){// If we already have this item instantiated, there is nothing// to do. This can happen when we are restoring the entire pager// from its saved state, where the fragment manager has already// taken care of restoring the fragments we previously had instantiated.if(mFragments.size()>position){Fragmentf=mFragments.get(position);if(f!=null){returnf;}}if(mCurTransaction==null){mCurTransaction=mFragmentManager.beginTransaction();}Fragmentfragment=getItem(position);if(DEBUG)Log.v(TAG,"Adding item #"+position+": f="+fragment);if(mSavedState.size()>position){Fragment.SavedStatefss=mSavedState.get(position);if(fss!=null){fragment.setInitialSavedState(fss);}}while(mFragments.size()<=position){mFragments.add(null);}fragment.setMenuVisibility(false);fragment.setUserVisibleHint(false);mFragments.set(position,fragment);mCurTransaction.add(container.getId(),fragment);returnfragment;}
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及以后:
java
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 |= PFLAG_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);
checkForLongClick(0);
}
break;
1
后者会调用setPressed方法,代码如下:
java
public void setPressed(boolean pressed) {
final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);
if (pressed) {
mPrivateFlags |= PFLAG_PRESSED;
} else {
mPrivateFlags &= ~PFLAG_PRESSED;
}
if (needsRefresh) {
refreshDrawableState();
}
dispatchSetPressed(pressed);
}
//viewgroup的实现
protected void dispatchSetPressed(boolean pressed) {
final View[] children = mChildren;
final int count = mChildrenCount;
for (int i = 0; i < count; i++) {
final View child = 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);
}
}
}