当用户点击某个项目时,我正在尝试将回收站视图项目滚动到屏幕顶部。
我的布局设置如下
- <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,获得添加滚动量,当你到达最后一个项目时,你将添加recyclelerView的translationY。
或者你可以添加一些虚拟Items.So他们可以查看你的正常Items的宽度和高度。应该这样做。
你有没有试过layoutManager的scrollToPositionWithOffset
函数:
thirdItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView
.getLayoutManager();
layoutManager.scrollToPositionWithOffset(0, 0);
}
});