问题
I have a RecyclerView that I want to start showing the bottom items, so I'm using:
myLayoutManager.setStackFromEnd(true).
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
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:
回答1:
From your description, I assume that you've set the RecyclerView height as match_parent.
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.
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.
Also, depending on what you're trying to achieve, you could also consider setReverseLayout(true). Hope this helps!
来源:https://stackoverflow.com/questions/44873389/android-only-setstackfromend-only-if-recycleview-list-is-larger-than-screen