将 recyclerview 项目滚动到滚动视图的顶部

scroll recyclerview item to top inside of scroll view

我正在尝试在用户单击项目时将回收站视图项目滚动到屏幕顶部。

我的布局设置如下

- <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())

感谢任何帮助。谢谢

你试过layoutManager的scrollToPositionWithOffset功能了吗:

thirdItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView
                    .getLayoutManager();
            layoutManager.scrollToPositionWithOffset(0, 0);
        }
});

在你的情况下,你没有足够的物品。

你不能让 recyclerView 滚动到低于最后一个项目所以要么你通过扩展它来实现你自己的 recyclerView,添加滚动量,当到达最后一个项目时你将添加 translationY recyclerView.

或者您可以添加一些虚拟对象 Items.So 它们可以是具有正常宽度和高度的视图 Items.That 应该可以做到。