首页 > 移动开发 > ANDROID > Android:嵌套滑动总结
public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull V child,
@NonNull View target, int dxConsumed, int dyConsumed,
int dxUnconsumed, int dyUnconsumed, @NestedScrollType int type) {
if (type == ViewCompat.TYPE_TOUCH) {
onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
dxUnconsumed, dyUnconsumed);
}
}
@Deprecated
public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout,
@NonNull V child, @NonNull View target, int dx, int dy, @NonNull int[] consumed) {
// Do nothing
}
public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout,
@NonNull V child, @NonNull View target, int dx, int dy, @NonNull int[] consumed,
@NestedScrollType int type) {
if (type == ViewCompat.TYPE_TOUCH) {
onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
}
}
public boolean onNestedFling(@NonNull CoordinatorLayout coordinatorLayout,
@NonNull V child, @NonNull View target, float velocityX, float velocityY,
boolean consumed) {
return false;
}
public boolean onNestedPreFling(@NonNull CoordinatorLayout coordinatorLayout,
@NonNull V child, @NonNull View target, float velocityX, float velocityY) {
return false;
}
public boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed,
@Nullable int[] offsetInWindow, @NestedScrollType int type) {
if (isNestedScrollingEnabled()) { //判断是否支持嵌套滑动,我们常常可以在应用中关闭嵌套滑动
ViewParentCompat.onNestedPreScroll(parent, mView, dx,
}
final Behavior behavior = lp.getBehavior();
//获取childview的behavior,然后调用behavior的onLayoutChild去布局。如果没有behavior就走CoordinatorLayout自身的onLayoutChild
if (behavior == null || !behavior.onLayoutChild(this, child, layoutDirection)) {
onLayoutChild(child, layoutDirection);
}
}
}
@Override
public boolean tryCaptureView(@NonNull View child, int pointerId) {
Log.i("fengfeng", "tryCaptureView start:" + "child=" + child + " pointId=" + pointerId + " state=" + state);
if (state == STATE_DRAGGING) {
Log.i("fengfeng", "tryCaptureView false 1");
return false;
}
if (touchingScrollingChild) {
Log.i("fengfeng", "tryCaptureView false 2");
return false;
}
if (state == STATE_EXPANDED && activePointerId == pointerId) {
View scroll = nestedScrollingChildRef != null ? nestedScrollingChildRef.get() : null;
if (scroll != null && scroll.canScrollVertically(-1)) {
// Let the content scroll up
Log.i("fengfeng", "tryCaptureView false 3");
return false;
}
}
Log.i("fengfeng", "tryCaptureView true 4");
return viewRef != null && viewRef.get() == child;
} @Override
public boolean tryCaptureView(@NonNull View child, int pointerId) {
Log.i("fengfeng", "tryCaptureView start:" + "child=" + child + " pointId=" + pointerId + " state=" + state);
if (state == STATE_DRAGGING) {
Log.i("fengfeng", "tryCaptureView false 1");
return false;
}
if (touchingScrollingChild) {
Log.i("fengfeng", "tryCaptureView false 2");
return false;
}
if (state == STATE_EXPANDED && activePointerId == pointerId) {
View scroll = nestedScrollingChildRef != null ? nestedScrollingChildRef.get() : null;
if (scroll != null && scroll.canScrollVertically(-1)) {
// Let the content scroll up
Log.i("fengfeng", "tryCaptureView false 3");
return false;
}
}
Log.i("fengfeng", "tryCaptureView true 4");
return viewRef != null && viewRef.get() == child;
}
case MotionEvent.ACTION_DOWN:
if (state != STATE_SETTLING) {
View scroll = nestedScrollingChildRef != null ? nestedScrollingChildRef.get() : null;
if (scroll != null && parent.isPointInChildBounds(scroll, initialX, initialY)) {
touchingScrollingChild = true;
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69983768/viewspace-2723155/ ,如需转载,请注明出处,否则将追究法律责任。