如何以编程方式在我的NestedScrollView中上下滚动,并带有一些延迟?

huangapple go评论64阅读模式
英文:

How do I scroll down and up in my Nestedscrollview programatically with some delay?

问题

我想要通过程序滚动一次向下,然后向上。滚动应该慢慢且平滑。这是我的代码:

scroll_1.post(() -> scroll_1.fullScroll(View.FOCUS_DOWN));
scroll_1.postDelayed(() -> {
    scroll_1.smoothScrollTo(0, 0);
}, 1000);

当我运行这段代码时,滚动速度很快,我该如何修复这个问题?

英文:

I want to scroll down once and then up programatically. The scrolling should be slow and smooth. This is my code:

scroll_1.post(() -> scroll_1.fullScroll(View.FOCUS_DOWN));
scroll_1.postDelayed(() -> {
    scroll_1.smoothScrollTo(0,0);
},1000);

When I run the code, it scrolls down and up so fast, how do I fix this?

答案1

得分: 0

要解决您的问题,请将以下语句替换为:

ObjectAnimator.ofInt(scroll_1, "scrollY", SCROLL_TO).setDuration(SCROLL_DURATION).start();

您需要将 SCROLL_TO 替换为您要滚动到的位置。如果要滚动到顶部,可以将值设置为 0

SCROLL_DURATION 是以毫秒为单位的持续时间,表示您希望 ScrollView 滚动到特定位置所需的时间。

如果要滚动到底部,您可以使用 ScrollView.getBottom()。示例代码如下:

ObjectAnimator.ofInt(scroll_1, "scrollY", scroll_1.getBottom()).setDuration(SCROLL_DURATION).start();
英文:

To solve your problem, replace the statement:

scroll_1.smoothScrollTo(0,0);

with the following:

ObjectAnimator.ofInt(scroll_1, "scrollY",  SCROLL_TO).setDuration(SCROLL_DURATION).start();

You have to replace SCROLL_TO with the position where you want to scroll to. If you want to scroll to the top, you can put 0 as the value.

SCROLL_DURATION is the duration in milliseconds that you want the ScrollView to take to scroll to a certain position.

If you want to scroll to the bottom, then you have to use ScrollView.getBottom(). Example code:

ObjectAnimator.ofInt(scroll_1, "scrollY",scroll_1.getBottom()).setDuration(SCROLL_DURATION).start();

huangapple
  • 本文由 发表于 2023年5月15日 14:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251435.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定