Parker
Parker

Reputation: 8851

Android only setStackFromEnd only if RecycleView/List is larger than screen

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:

What I Get What I Want

Upvotes: 6

Views: 4320

Answers (1)

Rohan Stark
Rohan Stark

Reputation: 2396

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!

Upvotes: 6

Related Questions