允许Recyclerview项目滚动到Recyclerview的顶部 [英] Allow Recyclerview Items to scroll past top of Recyclerview
问题描述
我有一个RecyclerView
列表,其中希望当前选择的项目显示在RecyclerView
的顶部中.但是,我仍然希望整个列表都是可滚动的,因此,从选定项目上方删除视图是不可能的解决方案.
I have a RecyclerView
list, in which I want the currently selected item to be shown at the top of the RecyclerView
. I still however, want the entire list to be scrollable, therefore, removing views from above the selected item is not a possible solution.
似乎我需要一种机制,其中RecyclerView
项能够滚动到RecyclerView
的范围之外.我不确定这是否可行,如果没有,是否有人可以确保当前选择的项目滚动到RecyclerView
的顶部.
It seems I need a mechanism where the RecyclerView
items are able to scroll beyond the bounds of the RecyclerView
. I'm not sure if this is possible, so if it is not, does anyone have a solution to ensuring the currently selected item scrolls to the top of the RecyclerView
.
我尝试过smoothScrollToPosition()
,但是在RecyclerView
的底部,并且希望其中一个中间项目滚动到顶部的情况下,此方法不起作用.
I have tried smoothScrollToPosition()
but this doesn't work in the case of being at the bottom of the RecyclerView
, and wanting one of the middle items to scroll to the top.
非常感谢
为了说明,我有4个项目的列表,由于列表中没有足够的项目,因此recyclerview无法滚动.
to Illustrate, I have a list of 4 items, the recyclerview cannot scroll as there is not enough items in the list.
然后我选择一个项目
然后我希望选定的项目滚动到顶部,但上面的项目仍可滚动.
I then want the selected item to scroll to top, but for the item above to still be scrollable.
所以,当我向上滚动...
So, when I scroll up...
推荐答案
在RecyclerView
上设置等于您的项目高度三倍的底部填充,然后设置android:clipToPadding="false"
.这将使您的底部项目滚动到顶部,并在底部项目(但仅在底部项目)上显示填充.
On the RecyclerView
set a bottom padding that is equal to three times your item's height then set android:clipToPadding="false"
. This will let your bottom item scroll to the top and show the padding on the bottom item but only on the bottom item.
这是一个答案,该问题很好地说明了该技术.
Here is an answer to a similar question that lays this technique out rather well.
这篇关于允许Recyclerview项目滚动到Recyclerview的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!