仅当RecycleView/List大于屏幕时,Android仅setStackFromEnd

编程入门 行业动态 更新时间:2024-10-25 22:23:58
本文介绍了仅当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 .在 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

本文发布于:2023-11-15 16:51:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596244.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:屏幕   List   RecycleView   Android   setStackFromEnd

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!