android - 将 recyclerview 项目滚动到滚动视图的顶部
问题描述
当用户单击某个项目时,我正在尝试将回收站视图项目滚动到屏幕顶部。
我的布局设置如下
- <FrameLayout>
-- <ScrollView>
--- <LinearLayout /> // static content
--- <RecyclerView /> // dynamic content
--- <LinearLayout /> // static content
-- </ScrollView>
-- </FrameLayout>
我想要实现的是当我点击一个 recyclerview 项目时。它应该向上滚动到屏幕顶部。
到目前为止,我已经尝试过,但没有运气。
->
// 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())
任何帮助表示赞赏。谢谢
解决方案
看起来在你的情况下你没有足够的物品。
你不能让recyclerView滚动低于最后一个项目所以要么你通过扩展它来实现你自己的recyclerView,获得添加滚动量,当到达最后一个项目时,你将添加recyclerView的translationY。
或者你可以添加一些虚拟项目。所以它们可以是视图,具有正常项目的宽度和高度。应该这样做。
推荐阅读
- slack-api - Slack 事件 API:从不接收“im_created”事件
- java - 在 ivy.xml 中为 Qmetry 框架项目添加依赖项以自动化移动原生应用程序的逻辑
- sql - 在关系数据库设计中将表连接到联结表的最佳实践是什么?
- xml - 如何在 xpath 3.1 中使用分析字符串
- python - 如何在 Python 或 matlab 中创建叠加气泡图?
- ios - 为什么 AVPlayer 定期观察者在设备上的开始时间之前显示当前时间?
- php - 如何访问值形式数组
- javascript - React JS 前端中的错误 Access-Control-Allow-Origin
- python - 我们如何近似计算运行一个程序需要多少内存?
- c++ - 在 C++ MFC 中按下 Tab 键时将焦点从 CIPAddressCtrl 转移到另一个控件