英文:
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论