2019-10-04 14:59:08 +02:00
|
|
|
package com.google.android.material.appbar;
|
2018-10-16 20:53:02 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
2019-09-20 11:02:16 +02:00
|
|
|
import android.graphics.Rect;
|
2018-10-16 20:53:02 +02:00
|
|
|
import android.util.AttributeSet;
|
2019-10-02 02:59:20 +02:00
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.widget.OverScroller;
|
2018-10-16 20:53:02 +02:00
|
|
|
|
2019-09-20 11:02:16 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.coordinatorlayout.widget.CoordinatorLayout;
|
|
|
|
|
2019-10-02 02:59:20 +02:00
|
|
|
import java.lang.reflect.Field;
|
2018-10-16 20:53:02 +02:00
|
|
|
|
2019-10-02 02:59:20 +02:00
|
|
|
// check this https://stackoverflow.com/questions/56849221/recyclerview-fling-causes-laggy-while-appbarlayout-is-scrolling/57997489#57997489
|
|
|
|
public final class FlingBehavior extends AppBarLayout.Behavior {
|
2018-10-16 20:53:02 +02:00
|
|
|
|
2019-09-20 11:02:16 +02:00
|
|
|
private final Rect focusScrollRect = new Rect();
|
|
|
|
|
2018-10-16 20:53:02 +02:00
|
|
|
public FlingBehavior(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
}
|
|
|
|
|
2019-09-20 11:02:16 +02:00
|
|
|
@Override
|
|
|
|
public boolean onRequestChildRectangleOnScreen(@NonNull CoordinatorLayout coordinatorLayout, @NonNull AppBarLayout child, @NonNull Rect rectangle, boolean immediate) {
|
|
|
|
focusScrollRect.set(rectangle);
|
|
|
|
|
|
|
|
coordinatorLayout.offsetDescendantRectToMyCoords(child, focusScrollRect);
|
|
|
|
|
|
|
|
int height = coordinatorLayout.getHeight();
|
|
|
|
|
|
|
|
if (focusScrollRect.top <= 0 && focusScrollRect.bottom >= height) {
|
|
|
|
// the child is too big to fit inside ourselves completely, ignore request
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dy;
|
|
|
|
|
|
|
|
if (focusScrollRect.bottom > height) {
|
|
|
|
dy = focusScrollRect.top;
|
|
|
|
} else if (focusScrollRect.top < 0) {
|
|
|
|
// scrolling up
|
|
|
|
dy = -(height - focusScrollRect.bottom);
|
|
|
|
} else {
|
|
|
|
// nothing to do
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int consumed = scroll(coordinatorLayout, child, dy, getMaxDragOffset(child), 0);
|
|
|
|
|
|
|
|
return consumed == dy;
|
|
|
|
}
|
|
|
|
|
2018-10-16 20:53:02 +02:00
|
|
|
@Override
|
2019-10-02 02:59:20 +02:00
|
|
|
public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
|
|
|
|
switch (ev.getActionMasked()) {
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
// remove reference to old nested scrolling child
|
|
|
|
resetNestedScrollingChild();
|
|
|
|
// Stop fling when your finger touches the screen
|
|
|
|
stopAppBarLayoutFling();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2018-10-16 20:53:02 +02:00
|
|
|
}
|
2019-10-02 02:59:20 +02:00
|
|
|
return super.onInterceptTouchEvent(parent, child, ev);
|
2018-10-16 20:53:02 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 02:59:20 +02:00
|
|
|
@Nullable
|
|
|
|
private OverScroller getScrollerField() {
|
|
|
|
try {
|
|
|
|
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass().getSuperclass();
|
|
|
|
if (headerBehaviorType != null) {
|
|
|
|
Field field = headerBehaviorType.getDeclaredField("scroller");
|
|
|
|
field.setAccessible(true);
|
|
|
|
return ((OverScroller) field.get(this));
|
2018-10-16 20:53:02 +02:00
|
|
|
}
|
2019-10-02 02:59:20 +02:00
|
|
|
} catch (NoSuchFieldException | IllegalAccessException e) {
|
|
|
|
// ?
|
2018-10-16 20:53:02 +02:00
|
|
|
}
|
2019-10-02 02:59:20 +02:00
|
|
|
return null;
|
2018-10-16 20:53:02 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 02:59:20 +02:00
|
|
|
@Nullable
|
|
|
|
private Field getLastNestedScrollingChildRefField() {
|
|
|
|
try {
|
|
|
|
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass();
|
|
|
|
if (headerBehaviorType != null) {
|
|
|
|
Field field = headerBehaviorType.getDeclaredField("lastNestedScrollingChildRef");
|
|
|
|
field.setAccessible(true);
|
|
|
|
return field;
|
|
|
|
}
|
|
|
|
} catch (NoSuchFieldException e) {
|
|
|
|
// ?
|
2018-10-16 20:53:02 +02:00
|
|
|
}
|
2019-10-02 02:59:20 +02:00
|
|
|
return null;
|
2018-10-16 20:53:02 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 02:59:20 +02:00
|
|
|
private void resetNestedScrollingChild(){
|
|
|
|
Field field = getLastNestedScrollingChildRefField();
|
|
|
|
if(field != null){
|
|
|
|
try {
|
|
|
|
Object value = field.get(this);
|
|
|
|
if(value != null) field.set(this, null);
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
// ?
|
2018-10-16 20:53:02 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-02 02:59:20 +02:00
|
|
|
}
|
2018-10-16 20:53:02 +02:00
|
|
|
|
2019-10-02 02:59:20 +02:00
|
|
|
private void stopAppBarLayoutFling() {
|
|
|
|
OverScroller scroller = getScrollerField();
|
|
|
|
if (scroller != null) scroller.forceFinished(true);
|
2018-10-16 20:53:02 +02:00
|
|
|
}
|
2019-10-02 02:59:20 +02:00
|
|
|
|
2018-10-16 20:53:02 +02:00
|
|
|
}
|