将recyclerview滚动到滚动视图的顶部 [英] scroll recyclerview item to top inside of scroll view

查看:135
本文介绍了将recyclerview滚动到滚动视图的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击某个项目时,我试图将回收者"视图项目滚动到屏幕顶部.

I'm trying to scroll recycler view item to top of screen when user clicks on an item.

我的布局设置如下

- <FrameLayout>
--   <ScrollView>
---     <LinearLayout /> // static content
---     <RecyclerView /> // dynamic content
---     <LinearLayout /> // static content
--   </ScrollView>
-- </FrameLayout>

我想要实现的是当我单击recyclerview的一项时.它应该向上滚动到屏幕顶部.

What I want to achieve is when i click on an item of recyclerview. It should scroll up to top of the screen.

到目前为止,我一直在尝试追随,但没有运气.

I have tried following so far but no luck.

-> 
  // use recycler view's layout manager to scroll.
  recyclerView.layoutManager.scrollToPositionWithOffset(position)

->
 // use smooth scroll to scroll
 recyclerView.smoothScrollToPosition(ItemPos)

->
  // use main scroll view to scroll on the screen. This kind of works but does not move item to top of the page.
  val y = recyclerView.y + recyclerView.getChildAt(position).y
  activity.binding.mainScrollview?.smoothScrollTo(0, y.toInt())

感谢您的帮助.谢谢

推荐答案

在您的情况下,您似乎没有足够的物品.

Looks like in your case you don't have enough items.

您不能使recyclerView滚动比最后一个项目低,因此,您可以通过扩展它来实现自己的recyclerView实现,获取添加滚动量,当到达最后一个项目时,您将累加recyclerView的translationY

You can't get recyclerView scroll lower than the last Item So it's either you make your own implementation of recyclerView by extending it,get add scroll amounts and when got to the last item you'll add up the translationY of the recyclerView.

或者您可以添加一些虚拟项目,因此它们可以是具有常规项目的宽度和高度的视图".应该做到这一点.

Or you can add a few dummy Items.So they can be View with width and height of your normal Items.That should do it.

这篇关于将recyclerview滚动到滚动视图的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆