停止 scrollView 在用户触摸时滚动
Stop scrollView scrolling on user touch
我自动将 scrollView 滚动到视图的底部。
scrollView.smoothScrollTo(0, scrollView.getBottom());
如果用户触摸布局,我需要滚动停止并停留在当前位置。
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//// TODO: 01/08/16 STOP SCROLLING
}
return false;
}
});
我试过 smoothScrollBy(0,0);
但没用。
一种方法可能是使用 smoothScrollToPosition,它会停止任何现有的滚动动作。请注意,此方法需要 API 级别 >= 8(Android 2.2,Froyo)。
请注意,如果当前位置远离所需位置,那么平滑滚动将花费相当长的时间并且看起来有点不稳定(至少在我对 Android 4.4 KitKat 的测试中)。我还发现调用 setSelection 和 smoothScrollToPosition 的组合有时会导致位置略微 "miss",这似乎只有在当前位置非常接近所需位置时才会发生。
在我的例子中,我希望我的列表在用户按下按钮时跳到顶部(位置=0)(这与你的用例略有不同,所以你需要根据你的需要进行调整) .
我用了下面的方法
private void smartScrollToPosition(ListView listView, int desiredPosition) {
// If we are far away from the desired position, jump closer and then smooth scroll
// Note: we implement this ourselves because smoothScrollToPositionFromTop
// requires API 11, and it is slow and janky if the scroll distance is large,
// and smoothScrollToPosition takes too long if the scroll distance is large.
// Jumping close and scrolling the remaining distance gives a good compromise.
int currentPosition = listView.getFirstVisiblePosition();
int maxScrollDistance = 10;
if (currentPosition - desiredPosition >= maxScrollDistance) {
listView.setSelection(desiredPosition + maxScrollDistance);
} else if (desiredPosition - currentPosition >= maxScrollDistance) {
listView.setSelection(desiredPosition - maxScrollDistance);
}
listView.smoothScrollToPosition(desiredPosition); // requires API 8
}
在按钮的操作处理程序中,我按如下方式调用它
case R.id.action_go_to_today:
ListView listView = (ListView) findViewById(R.id.lessonsListView);
smartScrollToPosition(listView, 0); // scroll to top
return true;
以上没有直接回答你的问题,但如果你能检测到当前位置何时位于或接近你想要的位置,那么也许你可以使用 smoothScrollToPosition 来停止滚动。
我用 ObjectAnimator
解决了这个问题。它不仅作为一种优雅的解决方案,而且还让我可以控制滚动速度。
我换了
scrollView.smoothScrollTo(0, scrollView.getBottom());
和
objectAnimator = ObjectAnimator
.ofInt(scrollView, "scrollY", scrollView.getBottom())
.setDuration(3000);
objectAnimator.start();
然后是
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
objectAnimator.cancel();
}
return false;
}
});
我自动将 scrollView 滚动到视图的底部。
scrollView.smoothScrollTo(0, scrollView.getBottom());
如果用户触摸布局,我需要滚动停止并停留在当前位置。
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//// TODO: 01/08/16 STOP SCROLLING
}
return false;
}
});
我试过 smoothScrollBy(0,0);
但没用。
一种方法可能是使用 smoothScrollToPosition,它会停止任何现有的滚动动作。请注意,此方法需要 API 级别 >= 8(Android 2.2,Froyo)。
请注意,如果当前位置远离所需位置,那么平滑滚动将花费相当长的时间并且看起来有点不稳定(至少在我对 Android 4.4 KitKat 的测试中)。我还发现调用 setSelection 和 smoothScrollToPosition 的组合有时会导致位置略微 "miss",这似乎只有在当前位置非常接近所需位置时才会发生。
在我的例子中,我希望我的列表在用户按下按钮时跳到顶部(位置=0)(这与你的用例略有不同,所以你需要根据你的需要进行调整) .
我用了下面的方法
private void smartScrollToPosition(ListView listView, int desiredPosition) {
// If we are far away from the desired position, jump closer and then smooth scroll
// Note: we implement this ourselves because smoothScrollToPositionFromTop
// requires API 11, and it is slow and janky if the scroll distance is large,
// and smoothScrollToPosition takes too long if the scroll distance is large.
// Jumping close and scrolling the remaining distance gives a good compromise.
int currentPosition = listView.getFirstVisiblePosition();
int maxScrollDistance = 10;
if (currentPosition - desiredPosition >= maxScrollDistance) {
listView.setSelection(desiredPosition + maxScrollDistance);
} else if (desiredPosition - currentPosition >= maxScrollDistance) {
listView.setSelection(desiredPosition - maxScrollDistance);
}
listView.smoothScrollToPosition(desiredPosition); // requires API 8
}
在按钮的操作处理程序中,我按如下方式调用它
case R.id.action_go_to_today:
ListView listView = (ListView) findViewById(R.id.lessonsListView);
smartScrollToPosition(listView, 0); // scroll to top
return true;
以上没有直接回答你的问题,但如果你能检测到当前位置何时位于或接近你想要的位置,那么也许你可以使用 smoothScrollToPosition 来停止滚动。
我用 ObjectAnimator
解决了这个问题。它不仅作为一种优雅的解决方案,而且还让我可以控制滚动速度。
我换了
scrollView.smoothScrollTo(0, scrollView.getBottom());
和
objectAnimator = ObjectAnimator
.ofInt(scrollView, "scrollY", scrollView.getBottom())
.setDuration(3000);
objectAnimator.start();
然后是
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
objectAnimator.cancel();
}
return false;
}
});