gpt4 book ai didi

android - 如何在 Android RecyclerView 和 LayoutManager 中使用 scrollVerticallyBy()?

转载 作者:可可西里 更新时间:2023-11-01 18:47:23 24 4
gpt4 key购买 nike

我想在我的回收 View 中沿 y 方向滚动一定数量的像素。我知道有 scrollToPosition()scrollToPositionWithOffset() 之类的方法。但是,这些并不能真正满足我的需求。

我看到 LayoutManager 提供了一个方法 scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state)。这听起来像我想要的。

我该如何使用它?什么是recyclerstate?我没有在网上找到任何示例,所以一些代码会很有用。

最佳答案

我不知道 scrollVerticallyBy,它看起来更像是 RecyclerView 的内部方法。您不只是在寻找方法 scrollBy(int x, int y)smoothScrollBy(int dx, int dy) 吗?

来自文档:

public void scrollBy (int x, int y)

Move the scrolled position of your view. This will cause a call to onScrollChanged(int, int, int, int) and the view will be invalidated.

Parameters
x int: the amount of pixels to scroll by horizontally
y int: the amount of pixels to scroll by vertically

您可以使用如下方法:

recyclerView.scrollBy(0, 50); //Scroll 50 pixels down
recyclerView.scrollBy(0, -50); //Scroll 50 pixels up

smoothScrollBy 也是如此。

关于android - 如何在 Android RecyclerView 和 LayoutManager 中使用 scrollVerticallyBy()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30683478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com