Skip to content

Commit deb988f

Browse files
committed
add fling fix test
1 parent 1977146 commit deb988f

7 files changed

Lines changed: 64 additions & 89 deletions

File tree

app/src/main/res/layout/activity_app_bar_fling_fix_layout.xml

Lines changed: 0 additions & 56 deletions
This file was deleted.

app/src/main/res/layout/activity_fling_fix_app_bar_layout.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@
4949
app:layout_anchorGravity="bottom|end"
5050
app:srcCompat="@android:drawable/ic_dialog_email"/>
5151

52-
<include layout="@layout/content_normal_app_bar_layout"/>
52+
<include layout="@layout/content_fling_fix_app_bar_layout"/>
5353

5454
</android.support.design.widget.CoordinatorLayout>

app/src/main/res/layout/activity_spring_app_bar_layout.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@
4949
app:layout_anchorGravity="bottom|end"
5050
app:srcCompat="@android:drawable/ic_dialog_email"/>
5151

52-
<include layout="@layout/content_spring_app_bar_layout"/>
52+
<include layout="@layout/content_fling_fix_app_bar_layout"/>
5353

5454
</android.support.design.widget.CoordinatorLayout>

app/src/main/res/layout/content_spring_app_bar_layout.xml renamed to app/src/main/res/layout/content_fling_fix_app_bar_layout.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.v4.widget.NestedScrollView
2+
<android.support.design.widget.NestedFixFlingScrollView
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
xmlns:app="http://schemas.android.com/apk/res-auto"
55
xmlns:tools="http://schemas.android.com/tools"
66
android:layout_width="match_parent"
77
android:layout_height="match_parent"
88
app:layout_behavior="@string/appbar_scrolling_view_behavior"
9-
tools:context="loopeer.com.appbarlayout_spring_extension.SpringAppBarLayoutActivity"
10-
tools:showIn="@layout/activity_spring_app_bar_layout">
9+
tools:context="loopeer.com.appbarlayout_spring_extension.NormalAppBarLayoutActivity"
10+
tools:showIn="@layout/activity_normal_app_bar_layout">
1111

1212
<TextView
1313
android:layout_width="wrap_content"
1414
android:layout_height="wrap_content"
1515
android:layout_margin="@dimen/text_margin"
1616
android:text="@string/large_text"/>
1717

18-
</android.support.v4.widget.NestedScrollView>
18+
</android.support.design.widget.NestedFixFlingScrollView>

app/src/main/res/layout/content_normal_app_bar_layout.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.design.widget.NestedFixFlingScrollView
2+
<android.support.v4.widget.NestedScrollView
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
xmlns:app="http://schemas.android.com/apk/res-auto"
55
xmlns:tools="http://schemas.android.com/tools"
@@ -15,4 +15,4 @@
1515
android:layout_margin="@dimen/text_margin"
1616
android:text="@string/large_text"/>
1717

18-
</android.support.design.widget.NestedFixFlingScrollView>
18+
</android.support.v4.widget.NestedScrollView>

appbarspring/src/main/java/android/support/design/widget/AppBarFlingFixBehavior.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,15 @@ public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child,
3535
mOffsetAnimator.cancel();
3636
}
3737

38-
// A new nested scroll has started so clear out the previous ref
39-
setLastNestedScrollingChildRef();
38+
setLastNestedScrollingChildRef(null);
4039
return started;
4140
}
4241

43-
private void setLastNestedScrollingChildRef() {
42+
private void setLastNestedScrollingChildRef(Object o) {
4443
try {
4544
Field field = AppBarLayout.Behavior.class.getDeclaredField("mLastNestedScrollingChildRef");
4645
field.setAccessible(true);
47-
field.set(this, null);
46+
field.set(this, o);
4847
} catch (Exception e) {
4948
e.printStackTrace();
5049
}
@@ -57,44 +56,38 @@ public boolean onNestedFling(final CoordinatorLayout coordinatorLayout,
5756
boolean flung = false;
5857

5958
if (!consumed) {
60-
// It has been consumed so let's fling ourselves
6159
flung = fling(coordinatorLayout, child, -child.getTotalScrollRange(),
6260
0, -velocityY);
6361
} else {
64-
// If we're scrolling up and the child also consumed the fling. We'll fake scroll
65-
// up to our 'collapsed' offset
6662
if (velocityY < 0) {
67-
// We're scrolling down
68-
final int targetScroll = /*-child.getTotalScrollRange()*/
63+
final int targetScroll =
6964
+ child.getDownNestedPreScrollRange();
7065
animateOffsetTo(coordinatorLayout, child, targetScroll, velocityY);
7166
flung = true;
72-
/*
73-
if (getTopBottomOffsetForScrollingSibling() < targetScroll) {
74-
// If we're currently not expanded more than the target scroll, we'll
75-
// animate a fling
76-
animateOffsetTo(coordinatorLayout, child, targetScroll, velocityY);
77-
flung = true;
78-
}
79-
*/
8067
} else {
81-
// We're scrolling up
8268
final int targetScroll = -child.getUpNestedPreScrollRange();
83-
animateOffsetTo(coordinatorLayout, child, targetScroll, velocityY);
84-
flung = true;
85-
/*if (getTopBottomOffsetForScrollingSibling() > targetScroll) {
86-
// If we're currently not expanded less than the target scroll, we'll
87-
// animate a fling
69+
if (getTopBottomOffsetForScrollingSibling() > targetScroll) {
8870
animateOffsetTo(coordinatorLayout, child, targetScroll, velocityY);
8971
flung = true;
90-
}*/
72+
}
9173
}
9274
}
9375

94-
// mWasNestedFlung = flung;
76+
setWasNestedFlung(flung);
9577
return flung;
9678
}
9779

80+
private void setWasNestedFlung(boolean o) {
81+
try {
82+
Field field = AppBarLayout.Behavior.class.getDeclaredField("mWasNestedFlung");
83+
field.setAccessible(true);
84+
field.set(this, o);
85+
} catch (Exception e) {
86+
e.printStackTrace();
87+
}
88+
}
89+
90+
9891
private void animateOffsetTo(final CoordinatorLayout coordinatorLayout,
9992
final AppBarLayout child, final int offset, float velocity) {
10093
final int distance = Math.abs(getTopBottomOffsetForScrollingSibling() - offset);

appbarspring/src/main/java/android/support/design/widget/AppBarLayoutSpringBehavior.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,44 @@ public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout
7272
checkShouldSpringRecover(coordinatorLayout, abl);
7373
}
7474

75+
@Override
76+
public boolean onNestedFling(final CoordinatorLayout coordinatorLayout,
77+
final AppBarLayout child, View target, float velocityX, float velocityY,
78+
boolean consumed) {
79+
boolean flung = false;
80+
81+
if (!consumed) {
82+
flung = fling(coordinatorLayout, child, -child.getTotalScrollRange(),
83+
0, -velocityY);
84+
} else {
85+
if (velocityY < 0) {
86+
final int targetScroll =
87+
+ child.getDownNestedPreScrollRange();
88+
animateOffsetTo(coordinatorLayout, child, targetScroll, velocityY);
89+
flung = true;
90+
} else {
91+
final int targetScroll = -child.getUpNestedPreScrollRange();
92+
if (getTopBottomOffsetForScrollingSibling() > targetScroll) {
93+
animateOffsetTo(coordinatorLayout, child, targetScroll, velocityY);
94+
flung = true;
95+
}
96+
}
97+
}
98+
99+
setWasNestedFlung(flung);
100+
return flung;
101+
}
102+
103+
private void setWasNestedFlung(boolean o) {
104+
try {
105+
Field field = AppBarLayout.Behavior.class.getDeclaredField("mWasNestedFlung");
106+
field.setAccessible(true);
107+
field.set(this, o);
108+
} catch (Exception e) {
109+
e.printStackTrace();
110+
}
111+
}
112+
75113
private void checkShouldSpringRecover(CoordinatorLayout coordinatorLayout, AppBarLayout abl) {
76114
if (mOffsetSpring > 0) animateRecoverBySpring(coordinatorLayout, abl);
77115
}

0 commit comments

Comments
 (0)