下面列出了android.support.v4.view.ViewCompat#NestedScrollType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Stop a nested scroll in progress.
*
* <p>This is a delegate method. Call it from your {@link android.view.View View} subclass
* method/{@link android.support.v4.view.NestedScrollingChild2} interface method with the same
* signature to implement the standard policy.</p>
*/
public void stopNestedScroll(@ViewCompat.NestedScrollType int type) {
ViewParent parent = getNestedScrollingParentForType(type);
if (parent != null) {
ViewParentCompat.onStopNestedScroll(parent, mView, type);
setNestedScrollingParentForType(type, null);
}
}
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow,
@ViewCompat.NestedScrollType int type) {
if (isNestedScrollingEnabled()) {
//获取实现了NestedScrollingParent2或NestedScrollingParent的祖辈控件,如果没有则为null
final ViewParent parent = getNestedScrollingParentForType(type);
if (parent == null) {
//没有实现NestedScrollingParent2或NestedScrollingParent的祖辈控件,则不作任何处理
return false;
}
if (dxConsumed != 0 || dyConsumed != 0 || dxUnconsumed != 0 || dyUnconsumed != 0) {
int startX = 0;
int startY = 0;
if (offsetInWindow != null) {
mView.getLocationInWindow(offsetInWindow);
startX = offsetInWindow[0];
startY = offsetInWindow[1];
}
//将嵌套滑动事件分发给祖辈控件的onNestedScroll方法
ViewParentCompat.onNestedScroll(parent, mView, dxConsumed,
dyConsumed, dxUnconsumed, dyUnconsumed, type);
if (offsetInWindow != null) {
mView.getLocationInWindow(offsetInWindow);
offsetInWindow[0] -= startX;
offsetInWindow[1] -= startY;
}
return true;
} else if (offsetInWindow != null) {
// No motion, no dispatch. Keep offsetInWindow up to date.
offsetInWindow[0] = 0;
offsetInWindow[1] = 0;
}
}
return false;
}
private ViewParent getNestedScrollingParentForType(@ViewCompat.NestedScrollType int type) {
switch (type) {
case TYPE_TOUCH:
return mNestedScrollingParentTouch;
case TYPE_NON_TOUCH:
return mNestedScrollingParentNonTouch;
}
return null;
}
private void setNestedScrollingParentForType(@ViewCompat.NestedScrollType int type, ViewParent p) {
switch (type) {
case TYPE_TOUCH:
mNestedScrollingParentTouch = p;
break;
case TYPE_NON_TOUCH:
mNestedScrollingParentNonTouch = p;
break;
}
}
public boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed,
@Nullable int[] offsetInWindow, @ViewCompat.NestedScrollType int type) {
if (isNestedScrollingEnabled()) {
//获取实现了NestedScrollingParent2或NestedScrollingParent的祖辈控件,如果没有则为null
final ViewParent parent = getNestedScrollingParentForType(type);
if (parent == null) {
//没有实现NestedScrollingParent2或NestedScrollingParent的祖辈控件,则不作任何处理
return false;
}
if (dx != 0 || dy != 0) {
int startX = 0;
int startY = 0;
if (offsetInWindow != null) {
mView.getLocationInWindow(offsetInWindow);
startX = offsetInWindow[0];
startY = offsetInWindow[1];
}
if (consumed == null) {
if (mTempNestedScrollConsumed == null) {
mTempNestedScrollConsumed = new int[2];
}
consumed = mTempNestedScrollConsumed;
}
consumed[0] = 0;
consumed[1] = 0;
//将嵌套滑动事件分发给祖辈控件的onNestedPreScroll方法
ViewParentCompat.onNestedPreScroll(parent, mView, dx, dy, consumed, type);
if (offsetInWindow != null) {
mView.getLocationInWindow(offsetInWindow);
offsetInWindow[0] -= startX;
offsetInWindow[1] -= startY;
}
return consumed[0] != 0 || consumed[1] != 0;
} else if (offsetInWindow != null) {
offsetInWindow[0] = 0;
offsetInWindow[1] = 0;
}
}
return false;
}
/**
* Check if this view has a nested scrolling parent view currently receiving events for
* a nested scroll in progress with the given type.
*
* <p>This is a delegate method. Call it from your {@link android.view.View View} subclass
* method/{@link android.support.v4.view.NestedScrollingChild} interface method with the same
* signature to implement the standard policy.</p>
*
* @return true if this view has a nested scrolling parent, false otherwise
*/
public boolean hasNestedScrollingParent(@ViewCompat.NestedScrollType int type) {
return getNestedScrollingParentForType(type) != null;
}
/**
* when scrooll, it would call back
*
* @param isUp isScroollUp
* @param dy child.getTanslationY
* @param type touch or not touch, TYPE_TOUCH, TYPE_NON_TOUCH
*/
void onScrollChange(boolean isUp, int dy, @ViewCompat.NestedScrollType int type);
/**
* when scrooll, it would call back
*
* @param isUp
* @param dy child.getTanslationY
* @param type
*/
void onScrollChange(boolean isUp, int dy, @ViewCompat.NestedScrollType int type);