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

不羁的心 提交于 2021-02-07 19:16:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!