仅当RecycleView/List大于屏幕时,Android仅setStackFromEnd [英] Android only setStackFromEnd only if RecycleView/List is larger than screen

查看:65
本文介绍了仅当RecycleView/List大于屏幕时,Android仅setStackFromEnd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 .在

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆