Skip to content

Instantly share code, notes, and snippets.

@NaikSoftware
Forked from karthikrg/gist:0948bf5d7864c0376bcd
Last active September 5, 2018 09:40
Show Gist options
  • Save NaikSoftware/47cefca5c60e408fa9beb209c4958686 to your computer and use it in GitHub Desktop.
Save NaikSoftware/47cefca5c60e408fa9beb209c4958686 to your computer and use it in GitHub Desktop.
Coordinator layout that supports nesting of other coordinator layouts within
package android.support.design.widget;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.NestedScrollingChildHelper;
import android.util.AttributeSet;
import android.view.View;
/**
* A CoordinatorLayout, except with changes to make it use less CPU time in some cases.
* To improve performance, use this class instead of CoordinatorLayout.
*
* <p>This class also has the ability to act as both as a Nested scrolling parent and child to consume
* and dispatch scroll callbacks to its parents. This allows it to be used in situations where multiple
* CoordinatorLayouts may be nested.</p>
*/
public class NestedCoordinatorLayout extends CoordinatorLayout {
private final NestedScrollingChildHelper nestedScrollingChildHelper;
public NestedCoordinatorLayout(@NonNull Context context) {
this(context, null);
}
public NestedCoordinatorLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public NestedCoordinatorLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
nestedScrollingChildHelper = new NestedScrollingChildHelper(this);
nestedScrollingChildHelper.setNestedScrollingEnabled(true);
}
/**
* The same as super.offsetChildToAnchor, except use less CPU time if the child is "gone".
* In this case, super.offsetChildToAnchor may cause several views to be redrawn.
* This can happen every display frame, so the app hogs CPU time, repeatedly redrawing the same views.
* This was reported as https://code.google.com/p/android/issues/detail?id=196879
*/
@Override
void offsetChildToAnchor(@NonNull View child, int layoutDirection) {
// Don't waste CPU time:
if (child.getVisibility() != View.GONE) {
super.offsetChildToAnchor(child, layoutDirection);
}
}
/**
* Default set to true
*/
public void setShouldConsumeAndForwardScrollEvents(boolean shouldConsumeAndForwardScrollEvents) {
nestedScrollingChildHelper.setNestedScrollingEnabled(shouldConsumeAndForwardScrollEvents);
}
@Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
boolean shouldAcceptNestedScroll = super.onStartNestedScroll(child, target, nestedScrollAxes);
if (nestedScrollingChildHelper.isNestedScrollingEnabled()) {
shouldAcceptNestedScroll |= nestedScrollingChildHelper.startNestedScroll(nestedScrollAxes);
}
return shouldAcceptNestedScroll;
}
@Override
public void onStopNestedScroll(View target) {
super.onStopNestedScroll(target);
if (nestedScrollingChildHelper.isNestedScrollingEnabled()) {
nestedScrollingChildHelper.onStopNestedScroll(target);
}
}
@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
if (nestedScrollingChildHelper.isNestedScrollingEnabled()) {
nestedScrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null);
}
}
@Override
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
super.onNestedPreScroll(target, dx, dy, consumed);
if (consumed[1] == 0 && nestedScrollingChildHelper.isNestedScrollingEnabled()) {
nestedScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, null);
}
}
@Override
public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
boolean didConsumeFling = super.onNestedFling(target, velocityX, velocityY, consumed);
if (nestedScrollingChildHelper.isNestedScrollingEnabled()) {
didConsumeFling |= nestedScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
}
return didConsumeFling;
}
@Override
public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
boolean didConsumePreFling = super.onNestedPreFling(target, velocityX, velocityY);
if (nestedScrollingChildHelper.isNestedScrollingEnabled()) {
didConsumePreFling |= nestedScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY);
}
return didConsumePreFling;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment