如何在 LinearLayoutManager - RecyclerView 中重叠项目(如堆叠卡片) [英] How to Overlap items in LinearLayoutManager - RecyclerView (like stacking cards)
问题描述
是否可以重叠 RecyclerView 中的项目?
我正在尝试使用 LinearLayoutManager.
我的要求和 LinearLayoutManager 一样,我只需要从上到下重叠里面的项目.(比如叠卡)
Is that possible to overlap the items from RecyclerView ?
I am trying that with LinearLayoutManager.
My requirements are just the same as in LinearLayoutManager and I just need to overlap the items inside from top to bottom. (like stacking cards)
我已经通过使用 ListView 看到了一些变体.所以,我想它会在 RecyclerView 中也是可能的.但是,经过一段时间的探索,我觉得实现自定义需要相当长的时间布局管理器和相当程度的理解.(我看着进入 Dave 关于构建自定义布局管理器 ) 的帖子>
I have seen some variant by using ListView. So, I figure it would be possible in RecyclerView too.But, after some times of exploration, I feel like its gonna take quite some time to implement a custom layout manager and quite an extent of understanding on this.(I looked into Dave's post about Building Custom LayoutManager )
所以,现在我想我可能只需要根据我的要求使用 ListView 变体,而不是处理自定义布局管理器的这种复杂性.
So, now I am thinking I might just need to use ListView variation based on my requirement instead of dealing with this much complexity of Custom Layout Manager.
但是,我只需要让里面的项目重叠.我觉得可能还有其他一些我还没有意识到的方向.请让我知道是否有任何除了上面的 ListView 变量和自定义布局管理器.
But, I just need to make the items inside to overlap. I feel like there might be some other direction that I haven't aware yet. Pls let me know if there is any apart from above ListView varient and Custom Layout Manager.
我也会在下面发布我的发现.
I will also post my findings below here.
推荐答案
我假设您正在寻找部分重叠(例如稍微散开的纸牌).如果是这样,使用 RecyclerView 和自定义 ItemDecoration 似乎相当简单.这是一个简单的例子,它垂直重叠项目 90 像素:
I assume you're looking for a partial overlap (e.g. deck of cards slightly fanned out). If so, this seems fairly simple with RecyclerView and a custom ItemDecoration. Here's a trivial example of one that overlaps the items by 90px vertically:
public class OverlapDecoration extends RecyclerView.ItemDecoration {
private final static int vertOverlap = -90;
@Override
public void getItemOffsets (Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
outRect.set(0, vertOverlap, 0, 0);
}
}
此示例对偏移量进行硬编码,但如果您的列表项的高度不同,您需要为此测量并添加逻辑.
This example hard codes the offset, but if your list items vary in height you'll need to measure and add logic for this.
在设置 layoutmanager 之前将此装饰添加到 RV.我已经用 StaggeredGrid 试过了,但它也应该适用于 LinearLayout 和 Grid LM.
Add this decoration to the RV before setting the layoutmanager. I've tried it with a StaggeredGrid but it should work with LinearLayout and Grid LM's as well.
这篇关于如何在 LinearLayoutManager - RecyclerView 中重叠项目(如堆叠卡片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!