简体   繁体   English

Kotlin/RecyclerView:scrollToPositionWithOffset 未显示

[英]Kotlin/RecyclerView: scrollToPositionWithOffset Not Showing Up

I wish to understand why I can't seem to use the scrollToPositionWithOffset method on a LinearLayoutManager?我想了解为什么我似乎无法在 LinearLayoutManager 上使用 scrollToPositionWithOffset 方法? Please see the image for what I mean :请看图片了解我的意思:

未找到 scrollToPositionWithOffset!<\/a>

A little background:一点背景:

The first line in the image (with scrollToPosition<\/code> ) is scrolling the RecyclerView to make the position (in this case 50) visible<\/strong> - this often means that the selected position shows up at the bottom of the visible RecyclerView (where position 50 first becomes visible after 'scrolling').图像中的第一行(带有scrollToPosition<\/code> )正在滚动 RecyclerView 以使位置(在本例中为 50)可见<\/strong>- 这通常意味着所选位置显示在可见 RecyclerView 的底部(其中位置 50 首先变为可见之后'滚动')。 Whereas I want to always show it at the top.而我想始终将其显示在顶部。 From my research, a some-what solution seems to be to use this scrollToPositionWithOffset<\/code> method ( Scroll RecyclerView to show selected item on top<\/a> )根据我的研究,某种解决方案似乎是使用此scrollToPositionWithOffset<\/code>方法( 滚动 RecyclerView 以在顶部显示所选项目<\/a>)

Interestingly, I was able to achieve what I wanted by customizing SmoothScroller of LinearLayoutManager, but my dataset is huge so speed of 'smooth scrolling' is an issue, and I can't seem increase the speed enough without causing other issues.有趣的是,我能够通过自定义 LinearLayoutManager 的 SmoothScroller 来实现我想要的,但是我的数据集很大,所以“平滑滚动”的速度是一个问题,而且我似乎无法在不引起其他问题的情况下提高速度。

In short, I'm hoping that scrollToPositionWithOffset will do the trick for me.简而言之,我希望 scrollToPositionWithOffset 能帮到我。 However, I don't know how to access the method.但是,我不知道如何访问该方法。

"

您需要投射从RecyclerView.getLayoutManager()返回的LayoutManager

(recyclerview.layoutManager as LinearLayoutManager).scrollToPositionWithOffset

Here is how:方法如下:

recyclerView.apply { 
      
    (layoutManager as LinearLayoutManager).scrollToPositionWithOffset(itemPosition, offsetValue)

}

Note that offsetValue could be X if your list/recycleView has horizontal orientation or Y if your list/recycleView has vertical orientation.请注意,如果您的 list/recycleView 具有水平方向,则offsetValue可能是X,如果您的 list/recycleView 具有垂直方向,则offsetValue可能是Y。

To provide a little more context to the other answers, scrollToPositionWithOffset()<\/code> is not a method of LayoutManager (the base class), but one of LinearLayoutManager (and its subclasses).为了为其他答案提供更多上下文, scrollToPositionWithOffset()<\/code>不是 LayoutManager (基类)的方法,而是 LinearLayoutManager (及其子类)之一。 My assumption is that the reasoning here was that such a method is not necessarily applicable (or could be ambiguous) for other LayoutManagers.我的假设是这里的推理是这样的方法不一定适用于其他 LayoutManagers(或可能是模棱两可的)。 Suppose we have a more "exotic" layout manager which does not organize items as a simple list, but rather in a circular shape.假设我们有一个更“异国情调”的布局管理器,它不会将项目组织为简单的列表,而是以圆形的形式。 What should the offset do in such a case?在这种情况下,偏移量应该怎么做? Does it represent an extra amount radians\/degrees to add to the scroll?它是否代表要添加到滚动条上的额外弧度\/度数? Or rather something else?或者更确切地说是别的什么?

In the case of a LinearLayoutManager (and any derived classes), however, it is fairly clear what the offset should do.然而,对于 LinearLayoutManager(和任何派生类),偏移量应该做什么是相当清楚的。 Please correct me if I'm wrong, but I think that's why only LinearLayoutManagers have access to the method, hence requiring a cast of the RecyclerView's LayoutManager.如果我错了,请纠正我,但我认为这就是为什么只有 LinearLayoutManagers 可以访问该方法,因此需要 RecyclerView 的 LayoutManager 的强制转换。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM