Skip to content

Commit

Permalink
* 修复可能的无法在越界的时候停止儿子滚动的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
voiddog committed May 30, 2018
1 parent 5b9b91a commit 403feb7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NestedDampLayoutActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nested_damp_layout)
val adapter = TestAdapter()
for (i in 1..5) {
for (i in 1..50) {
adapter.contentList.add("我是${i}")
}
rec_list.layoutManager = LinearLayoutManager(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
Expand Down Expand Up @@ -42,6 +44,9 @@ public class DampViewUtil {
public static void stopScroll(View view) {
if (view instanceof RecyclerView) {
((RecyclerView) view).stopScroll();
} else if (view instanceof NestedScrollingChild){
ViewCompat.stopNestedScroll(view);
ViewCompat.stopNestedScroll(view, ViewCompat.TYPE_NON_TOUCH);
} else {
MotionEvent ev = MotionEvent.obtain(System.currentTimeMillis(), System.currentTimeMillis()
, MotionEvent.ACTION_DOWN, 0, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,12 @@ public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int
if (offset > minFlingOffset) {
// head over scroll
consumed[1] = scroll(dy);
} else if (type == ViewCompat.TYPE_NON_TOUCH && !target.canScrollVertically(1)) {
// bottom over scroll but scroll down, the nested target should stop scroll
DampViewUtil.stopScroll(target);
}
} else if (dy < 0 && (dampFlag&DAMP_FLAG_END)==DAMP_FLAG_END){
// scroll up
if (offset < maxFlingOffset) {
// bottom over scroll
consumed[1] = scroll(dy);
} else if (type == ViewCompat.TYPE_NON_TOUCH && !target.canScrollVertically(-1)) {
// head over scroll but scroll up, the nested target should stop scroll
DampViewUtil.stopScroll(target);
}
}
// record motionY event
Expand Down Expand Up @@ -239,15 +233,17 @@ public void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed,
applyNestedVelocity();
playAnim();
}
if (dy > 0 && !target.canScrollVertically(1)) {
// 越界,而且有未消耗滚动,表示无法继续往下滚了
if (dy > 0 && dyUnconsumed > 0) {
DampViewUtil.stopScroll(target);
}
} else if (offset > maxFlingOffset) {
if (!animation.isRunning()) {
applyNestedVelocity();
playAnim();
}
if (dy < 0 && !target.canScrollVertically(-1)) {
// 越界,而且有未消耗滚动,表示无法继续往下滚了
if (dy < 0 && dyUnconsumed < 0) {
DampViewUtil.stopScroll(target);
}
}
Expand Down

0 comments on commit 403feb7

Please sign in to comment.