我有一个RecyclerView,我想开始显示最下面的项目,因此我正在使用: myLayoutManager.setStackFromEnd(true).
I have a RecyclerView that I want to start showing the bottom items, so I'm using: myLayoutManager.setStackFromEnd(true).
当有足够的项目填满屏幕时,它会按照我的意愿工作,但是如果RecyclerView中只有少量项目,它将向下推并将它们留在屏幕顶部
It works just as I want when there are enough items to fill the screen, but if there are only a few items in the RecyclerView, it pushes them down and leaves empty space at the top of the screen
有什么方法可以防止这种情况吗?如果RecyclerView大于屏幕高度,则只能阻止 setStackFromEnd 吗?
Is there any way prevent this, or to only setStackFromEnd if the RecyclerView is larger than the screen height?
这是我得到的和我想要得到的东西的一个例子:
Here's an example of what I get and what I'm trying to get:
推荐答案
根据您的描述,我假设您已将 RecyclerView 的高度设置为 match_parent .
From your description, I assume that you've set the RecyclerView height as match_parent.
因此,因此,您看到的只是常规行为.当 setStackFromEnd 未设置为 true 时,您的 RecyclerView 从顶部开始填充其视图(如图所示).但是,当您从末端设置堆栈时,会发生相反的行为.因此,您会在顶部看到空白区域.
So, because of this, what you're seeing is just the regular behaviour. When setStackFromEnd is not set to true, your RecyclerView populates its views from the top (as shown in the image). However, when you set the stack from end, the opposite behaviour occurs. And, hence, you see the empty space at the top.
要删除空白区域,建议您将 RecyclerView 布局高度设置为 wrap_content .在 Android支持库23.2 .因此,您的 RecyclerView 布局将仅根据子视图的数量占用其所需的空间.
For removing the empty space, I'd suggest that you set your RecyclerView layout height to wrap_content. wrap_content was introduced for RecyclerView in Android Support Library 23.2. Because of this, your RecyclerView layout would only take up the space that it needs according to the number of child views.
此外,根据您要实现的目标,您还可以考虑 setReverseLayout(true).希望这会有所帮助!
Also, depending on what you're trying to achieve, you could also consider setReverseLayout(true). Hope this helps!
更多推荐
仅当RecycleView/List大于屏幕时,Android仅setStackFromEnd
发布评论