Skip to content

Instantly share code, notes, and snippets.

@AlexMahao
Last active September 7, 2018 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexMahao/1168720fdb99f76d0f3e96ff4246c9ad to your computer and use it in GitHub Desktop.
Save AlexMahao/1168720fdb99f76d0f3e96ff4246c9ad to your computer and use it in GitHub Desktop.
Fixed AppBarLayout+RecyclerView fling bug
package your.package;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.OverScroller;
import java.lang.reflect.Field;
public class NoBounceBehavior extends AppBarLayout.Behavior {
private OverScroller mHeaderScroller;
public NoBounceBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
findHeaderScroller(context);
}
private void findHeaderScroller(Context context) {
if (mHeaderScroller != null)
return;
mHeaderScroller = new OverScroller(context);
try {
Class<?> clzHeaderBehavior = getClass().getSuperclass().getSuperclass();
Field fieldScroller = clzHeaderBehavior.getDeclaredField("mScroller");
fieldScroller.setAccessible(true);
fieldScroller.set(this, mHeaderScroller);
} catch (Exception e) {}
}
@Override
public void onNestedScrollAccepted(@NonNull CoordinatorLayout coordinatorLayout, @NonNull AppBarLayout child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
super.onNestedScrollAccepted(coordinatorLayout, child, directTargetChild, target, axes, type);
if (mHeaderScroller != null && mHeaderScroller.computeScrollOffset()) {
mHeaderScroller.forceFinished(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment