Last active
September 7, 2018 02:52
-
-
Save AlexMahao/1168720fdb99f76d0f3e96ff4246c9ad to your computer and use it in GitHub Desktop.
Fixed AppBarLayout+RecyclerView fling bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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